MASA MAUI Plugin (七)應用通知角標(小紅點)Android+iOS

背景

MAUI的出現,賦予了廣大Net開發者開發多平臺應用的能力,MAUI 是Xamarin.Forms演變而來,但是相比Xamarin性能更好,可擴展性更強,結構更簡單。但是MAUI對于平臺相關的實現并不完整。所以MASA團隊開展了一個實驗性項目,意在對微軟MAUI的補充和擴展

項目地址https://github.com/BlazorComponent/MASA.Blazor/tree/feature/Maui/src/Masa.Blazor.Maui.Plugin

每個功能都有單獨的demo演示項目,考慮到app安裝文件體積(雖然MAUI已經集成裁剪功能,但是該功能對于代碼本身有影響),屆時每一個功能都會以單獨的nuget包的形式提供,方便測試,現在項目才剛剛開始,但是相信很快就會有可以交付的內容啦。

前言

本系列文章面向移動開發小白,從零開始進行平臺相關功能開發,演示如何參考平臺的官方文檔使用MAUI技術來開發相應功能。

介紹

上一篇文章我們集成了個推的消息通知,那么消息到達移動端之后,除了會在通知欄顯示之外,在應用的角標也會顯示未讀消息的數量(小紅點),然后用戶點擊查看消息之后,這些數字角標也可以自動消除,這個功能在MAUI中如何實現呢?

一、iOS部分

思路

https://developer.apple.com/documentation/uikit/uiapplication/1622918-applicationiconbadgenumber

我們參考一下官方文檔,UIApplication下有一個applicationIconBadgeNumber的屬性

var applicationIconBadgeNumber: Int { get set }

我們只需要給這個屬性賦值具體的整數即可

https://developer.apple.com/documentation/uikit/uiapplication/1622975-shared

我們可以通過shared獲取當前UIApplication的實例,然后就可以給applicationIconBadgeNumber賦值了,但是如果你直接這樣做,你會發現并沒有效果,因為 iOS 8 以后,需要注冊用戶通知,以獲得用戶的授權。

https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649527-requestauthorization

我們可以通過UNUserNotificationCenter的RequestAuthorization方法獲取請求用戶本地和遠程的通知權限。

878c7aee3757f16bd1d5efea440fcb3a.jpeg

開發步驟

我們新建一個目錄Badger,并在下面新建MAUI類庫項目Masa.Blazor.Maui.Plugin.Badger,在Platforms下的iOS文件夾新建MasaMauiBadgerService部分類

using UIKit;
using UserNotifications;
namespace Masa.Blazor.Maui.Plugin.Badger
{public static partial class MasaMauiBadgerService{private static void PlatformSetNotificationCount(int count){// Requests the user’s authorization to allow local and remote notifications for your app.UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Badge, (r, e) =>{});// The number currently set as the badge of the app icon on the Home screen// Set to 0 (zero) to hide the badge number. The default value of this property is 0.UIApplication.SharedApplication.ApplicationIconBadgeNumber = count;}}
}

RequestAuthorization方法有兩個參數

1、UNAuthorizationOptions?代表應用請求的授權選項,這里我們使用Badge
2、completionHandle?這是一個Action,有兩個參數,第一個參數是一個bool值,代表是否已授予授權,第二個參數是一個NSError類型,表示包含錯誤信息或未發生錯誤的對象。我們這里暫不處理出錯的情況

我們通過UIApplication.SharedApplication獲取當前的UIApplication實例,然后直接給ApplicationIconBadgeNumber?賦值,這里如果我們想清除角標,就直接賦值0即可。

我們繼續在項目根目錄新建MasaMauiBadgerService類,通過SetNotificationCount來調用不同平臺的PlatformSetNotificationCount方法。

namespace Masa.Blazor.Maui.Plugin.Badger
{// All the code in this file is included in all platforms.public static partial class MasaMauiBadgerService{public static void SetNotificationCount(int count){PlatformSetNotificationCount(count);}}
}

二、安卓部分

思路

安卓部分比iOS相對復雜,我們本著不造輪子的思想,找了一個現成的aar包,ShortcutBadger

項目maven地址:https://repo1.maven.org/maven2/me/leolin/ShortcutBadger

開發步驟

1、我們下載最新的ShortcutBadger-1.1.22.aar包,并新建Android Java 庫綁定項目Masa.Blazor.Maui.Plugin.BadgerBinding

d811bc84c1b744284be038ce27c2fe38.png

在根目錄創建Jars文件夾,并將下載的aar文件添加進去。添加進去的文件屬性中,生成操作默認選擇的是AndroidLibrary,如果不對請手動更正。
右鍵生成這個項目,這里很順利沒有任何報錯。

2、我們在Masa.Blazor.Maui.Plugin.Badger項目中引用Masa.Blazor.Maui.Plugin.BadgerBinding項目,由于我們只有在安卓平臺需要項目引用,所以我們手動修改一下項目文件中的引用部分,添加Android平臺的判斷。

<ItemGroup Condition="'$(TargetFramework)' == 'net7.0-android'"><ProjectReference Include="..\Masa.Blazor.Maui.Plugin.BadgerBinding\Masa.Blazor.Maui.Plugin.BadgerBinding.csproj" /></ItemGroup>

3、從 Android 8.0(API 級別 26)開始,所有通知都必須分配到相應的渠道,關于通知通道的信息,可以參考以下官方文檔

https://developer.android.google.cn/training/notify-user/channels?hl=zh-cn

Java 代碼private void createNotificationChannel() {// Create the NotificationChannel, but only on API 26+ because// the NotificationChannel class is new and not in the support libraryif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {CharSequence name = getString(R.string.channel_name);String description = getString(R.string.channel_description);int importance = NotificationManager.IMPORTANCE_DEFAULT;NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);channel.setDescription(description);// Register the channel with the system; you can't change the importance// or other notification behaviors after thisNotificationManager notificationManager = getSystemService(NotificationManager.class);notificationManager.createNotificationChannel(channel);}}

我們參照以上寫法,在Masa.Blazor.Maui.Plugin.Badger項目的Android平臺目錄下新建MasaMauiBadgerService?類,添加一個CreateNotificationChannel方法

using Android.App;
using AndroidX.Core.App;namespace Masa.Blazor.Maui.Plugin.Badger
{// All the code in this file is included in all platforms.public static partial class MasaMauiBadgerService{private static void CreateNotificationChannel(){if (OperatingSystem.IsAndroidVersionAtLeast(26)){using var channel = new NotificationChannel($"{Android.App.Application.Context.PackageName}.channel", "Notification channel", NotificationImportance.Default){Description = "Masa notification channel"};var notificationManager = NotificationManager.FromContext(Android.App.Application.Context);notificationManager?.CreateNotificationChannel(channel);}}}
}

1、通過OperatingSystem.IsAndroidVersionAtLeast來判斷當前的Android版本。
2、NotificationChannel的創建方式與Java一致,三個參數分別為ChannelID,name、Importance,這里注意第三個參數代表重要性級別,我們這里使用了NotificationImportance.Default。

用戶可見的重要性級別重要性(Android 8.0 及更高版本)
緊急:發出提示音,并以浮動通知的形式顯示IMPORTANCE_HIG
高:發出提示音IMPORTANCE_DEFAULT
中:無提示音IMPORTANCE_LOW
低:無提示音,且不會在狀態欄中顯示IMPORTANCE_MIN

3、Description?指定用戶在系統設置中看到的說明。
4、通過NotificationManager.FromContext?創建?notificationManager,然后調用CreateNotificationChannel來創建通知通道。

我們繼續添加SetNotificationCount方法

private static void PlatformSetNotificationCount(int count){ME.Leolin.Shortcutbadger.ShortcutBadger.ApplyCount(Android.App.Application.Context, count);NotificationCompat.Builder builder = new(Android.App.Application.Context, $"{Android.App.Application.Context.PackageName}.channel");builder.SetNumber(count);builder.SetContentTitle(" ");builder.SetContentText("");builder.SetSmallIcon(Android.Resource.Drawable.SymDefAppIcon);var notification = builder.Build();var notificationManager = NotificationManager.FromContext(Android.App.Application.Context);CreateNotificationChannel();notificationManager?.Notify((int)DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), notification);}

1、調用ShortcutBadger的ApplyCount方法來添加角標
2、創建NotificationCompat.Builder示例,并以此設置角標顯示數量(SetNumber),通知的標題(SetContentTitle)和內容(SetContentText),以及通知圖標(SetSmallIcon)。
3、調用我們剛寫好的方法創建通知通道。
4、通過NotificationManager.Notify方法在狀態欄發布一條通知。
該方法有兩個參數,第一個參數是一個int類型id,這個id是通知的標識符,在應用程序中應該唯一。這里需要注意:如果你發布了相同id的通知,并且前一個并沒有取消,那么該id對應的通知會被更新。第二個參數是一個notification 對象,是通過NotificationCompat.Builder創建出來的。

三、創建Demo項目

1、新建一個MAUI Blazor項目:BadgerSample,添加對Masa.Blazor.Maui.Plugin.Badger項目的引用
2、添加Android權限:修改Android平臺目錄中的AndroidManifest.xml文件,添加必要的權限。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"><application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.POST_NOTIFICATIONS"/><uses-permission android:name="android.permission.READ_APP_BADGE" /><uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/><uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS"/><uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /><uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
</manifest>

注意:國內不同手機廠家可能需要額外的權限配置,需要參考具體廠家的配置說明。

3、修改Index.razor文件

實際的使用場景應該是移動端接收消息推送,在處理消息推送的方法內修改角標,我們這里為了簡化,在頁面直接通過按鈕觸發修改角標顯示的數量。

@page "/"
@using Masa.Blazor.Maui.Plugin.Badger;<h1>Masa blazor badger sample</h1>Masa blazor badger sample.<button @onclick="OnIncrementClicked">Add</button>
<button @onclick="OnClearClicked">Clear</button>
@code{int count;private void OnIncrementClicked(){count++;MasaMauiBadgerService.SetNotificationCount(count);}private void OnClearClicked(){count = 0;MasaMauiBadgerService.SetNotificationCount(count);}
}

Android 演示:演示機:vivo x70 pro+

da9511c0e3f22883e2347146de6c6134.gif
iOS 演示:演示機:iPhone 14 iOS16 模擬器

efccf624f48dceca0c80627704bb77fd.gif


如果你對我們的開源項目感興趣,無論是代碼貢獻、使用、提 Issue,歡迎聯系我們

194146ff9516e2f036fa022e29f58901.png

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/279842.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279842.shtml
英文地址,請注明出處:http://en.pswp.cn/news/279842.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

SAP如何查看會計憑證

比如SAP中已經存在著很多會計憑證&#xff0c;你想要進入SAP隨便看看會計憑證的列表&#xff0c;怎么操作呢&#xff1f;事務碼 IDCNDOC運行結果看到了憑證們&#xff0c;和每個憑證的行項目們上圖看到的結果比較凌亂實際上我們重新進入IDCNDOC可以通過輸入的勾選&#xff0c;選…

Spring Data Redis與Jedis的選擇(轉)

說明&#xff1a;內容可能有點舊&#xff0c;需要在業務上做權衡。 Redis的客戶端有兩種實現方式&#xff0c;一是可以直接調用Jedis來實現&#xff0c;二是可以使用Spring Data Redis&#xff0c;通過Spring的封裝來調用。應該使用哪一個呢&#xff1f;基于當前版本Spring Dat…

C# 溫故而知新:Stream篇(五)

MemoryStream 目錄&#xff1a; 1 簡單介紹一下MemoryStream 2 MemoryStream和FileStream的區別 3 通過部分源碼深入了解下MemoryStream 4 分析MemorySteam最常見的OutOfMemory異常 5 MemoryStream 的構造 6 MemoryStream 的屬性 7 MemoryStream 的方法 8 MemoryStream 簡單示例…

dosbox 自動運行_如何使用DOSBox運行DOS游戲和舊應用

dosbox 自動運行New versions of Windows don’t fully support classic DOS games and other old applications — this is where DOSBox comes in. It provides a full DOS environment that runs ancient DOS apps on modern operating systems. Windows的新版本不完全支持經…

WPF 自定義放大鏡控件

控件名&#xff1a;Magnifier作 者&#xff1a;WPFDevelopersOrg - 驚鏵原文鏈接[1]&#xff1a;https://github.com/WPFDevelopersOrg/WPFDevelopers框架使用.NET40&#xff1b;Visual Studio 2019;實現此功能需要用到 VisualBrush &#xff0c;放大鏡展現使用 Canvas ->…

springboot小筆記

如果默認通過IDEA的springboot 插件布置的 的初始啟動類是這樣的&#xff0c;這種就是一個普通的java類&#xff0c;只能以jar打包 package com.how2java.springboot;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.Sprin…

.NET實現之(WebBrowser數據采集—續篇)

我們繼續“.NET實現之(WebBrowser數據采集)“系列篇之最后一篇&#xff0c;這篇本人打算主要講解怎么用WebBrowser控件來實現“虛擬”的交互性程序&#xff1b;比如我們用Winform做為宿主容器&#xff0c;用Asp.net做相關收集程序頁面&#xff0c;我們需要通過客戶端填寫相關數…

ipad和iphone切圖_如何在iPhone,iPad和Mac上使消息靜音

ipad和iphone切圖If you use Messages on your iPhone, iPad, or Mac, then you probably know how quickly you can become overrun with message notifications, especially if you’re part of a group message. Thankfully, there’s an easy way to mute specific message…

Pipy 實現 SOCKS 代理

上篇我們介紹了服務網格 osm-edge 出口網關使用的 HTTP 隧道&#xff0c;其處理方式與另一種代理有點類似&#xff0c;就是今天要介紹的 SOCKS 代理。二者的主要差別簡單來說就是前者使用 HTTP CONNECT 告知代理目的地址&#xff0c;而后者則是通過 SOCKS 協議。值得一提的是&a…

python拓展7(Celery消息隊列配置定時任務)

介紹 celery 定時器是一個調度器&#xff08;scheduler&#xff09;&#xff1b;它會定時地開啟&#xff08;kicks off&#xff09;任務&#xff0c;然后由集群中可用的工人&#xff08;worker&#xff09;來執行。 定時任務記錄&#xff08;entries&#xff09;默認 從 beat_s…

Asia Yokohama Regional Contest 2018 G題 What Goes Up Must Come Down(樹狀數組求逆序對)

https://codeforces.com/gym/102082 題意&#xff1a; 給一個數組大小不超過1e5&#xff0c;每個數的值也是1e5以內&#xff0c;可以交換相鄰兩個數&#xff0c;求保證它呈現一個非遞減再非遞增的趨勢的最小交換次數。 題解&#xff1a;對每個數來說&#xff0c;只有兩種情況&a…

Android系統的開機畫面顯示過程分析(8)

3. 第三個開機畫面的顯示過程第三個開機畫面是由應用程序bootanimation來負責顯示的。應用程序bootanimation在啟動腳本init.rc中被配置成了一個服務&#xff0c;如下所示&#xff1a;service bootanim /system/bin/bootanimation user graphics group graphics disabled o…

chrome連接已重置_如何重置(或調整)Chrome的下載設置

chrome連接已重置By default, Chrome saves all downloaded files to the same location—a dedicated “Downloads” folder. The thing is, this isn’t always practical for all types of download files. The good news is you can easily tweak this setting. 默認情況下…

.Net 7 團隊把國內的龍芯確實當做一等公民和棄用的項目

楔子&#xff1a;國內龍芯據說是用的自己的指令集&#xff0c;在研究ILC的時候&#xff0c;發現了龍芯在微軟那邊確實是一等公民的存在。同X64,ARM,X86一同并列交叉編譯和二進制提取。龍芯官網龍芯平臺.NET&#xff0c;是龍芯公司基于開源社區.NET獨立研發適配的龍芯版本&#…

戴爾押寶iSCSI,由低到高組合成型

戴爾&#xff08;Dell&#xff09;是較早接受SAS技術的主流存儲廠商之一&#xff0c;2006年已推出采用SAS硬盤驅動器的SAS直連存儲&#xff08;DAS&#xff09;系統PowerVault MD3000。一年之后&#xff0c;主機連接改用iSCSI的PowerVault MD3000i問世。2008年1月&#xff0c;E…

仿Gin搭建自己的web框架(七)

本篇介紹HTTP Basic Auth的實現以及Recovery機制。 HTTP Basic Auth Basic Auth是一種開放平臺認證方式&#xff0c;簡單的說就是需要你輸入用戶名和密碼才能繼續訪問。對于Basic Auth的概念不過多的進行介紹&#xff0c;直接進入如何實現的過程。 Basic Auth說白了就是賬號和密…

canvas高斯模糊算法

對于模糊圖片這個效果的實現&#xff0c;其實css3中的filter屬性也能夠實現&#xff0c;但是這個屬性的兼容性不是很好&#xff0c;所以我們通常不用這種方法實現&#xff0c;而使用canvas配合JS實現。 <span style"white-space:pre"> </span>//高斯模糊…

word中插入公式的快捷鍵_如何使用插入鍵在Word中插入復制的內容

word中插入公式的快捷鍵In Word, the “Insert” key on the keyboard can be used to switch between Insert and Overtype modes. However, it can also be used as a shortcut key for inserting copied or cut content at the current cursor position. 在Word中&#xff0…

微軟終于為 Visual Studio 添加了內置的 Markdown 編輯器

微軟終于為 Visual Studio 添加了內置的 Markdown 編輯器。根據官方博客的介紹&#xff0c;由于收到許多用戶的反饋&#xff0c;微軟決定為 Visual Studio 添加 Markdown 編輯器。開發者下載最新的 Visual Studio 17.5 第 2 個預覽版就能夠使用 Markdown 編輯功能&#xff0c;無…

【經驗分享】Hydra(爆破神器)使用方法

這個也是backtrack下面很受歡迎的一個工具 參數詳解&#xff1a;-R 根據上一次進度繼續破解-S 使用SSL協議連接-s 指定端口-l 指定用戶名-L 指定用戶名字典(文件)-p 指定密碼破解-P 指定密碼字典(文件)-e 空密碼探測和指定用戶密碼探測(ns)-C 用戶名可以用:分割(username:passw…