一、使用Notification
1、創(chuàng)建一個通知
1.1 注冊一個渠道
在Android13,版本通知的使用發(fā)生了新的變化。
1.1.1 NotificationManager原生類
首先我們需要創(chuàng)建一個NotificationManager
用于管理通知。NotificationManager
僅支持在 API 等級 11(Android 3.0)及以上的設(shè)備上使用,因此在較舊的 Android 版本上無法使用較新的通知功能。
//創(chuàng)建notificationManager對通知進行管理
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
`1.1.2 NotificationManagerCompat兼容類
NotificationManagerCompat
是 Android Support Library(現(xiàn)在是 AndroidX 庫)中提供的通知管理兼容類。它用于支持在各個 API 等級的設(shè)備上管理通知,并提供了一致的通知管理接口,無需手動進行版本適配。
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
這兩種方式任意選擇一種即可。
接下來需要注冊一個渠道(channel),通知渠道是一種對通知進行分類和管理的機制。
// 在 MainActivity 或其他合適的地方創(chuàng)建通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "001"; //通知渠道的標(biāo)識符
CharSequence channelName = "QQ"; //通知渠道的位置
String channelDescription = "來自QQ好友的消息"; //通知渠道的描述
//設(shè)置通知渠道的級別
int importance = NotificationManager.IMPORTANCE_DEFAULT;
//創(chuàng)建通知渠道
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.setDescription(channelDescription);//可以省略
//在系統(tǒng)中注冊消息
notificationManager.createNotificationChannel(notificationChannel);
}
通過 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
條件判斷,確保當(dāng)前設(shè)備運行的 Android 版本是 8.0 或更高版本。只有在這種情況下,通知渠道才會被創(chuàng)建。
然后,定義了三個變量和設(shè)置了一個渠道級別用于設(shè)置通知渠道的屬性:
參數(shù) | 說明 |
---|---|
channelId | 通知渠道的ID,用戶不可見,實例化Notification 的時候需要用到。 |
channelName | 通知渠道的名稱,這個是方便用戶管理通知用的,用戶可見 |
channelDescription | 通知渠道的描述。用于在系統(tǒng)設(shè)置界面和通知管理中向用戶顯示通知渠道的詳細描述。 |
importance | 渠道優(yōu)先級 |
渠道的優(yōu)先級有三種:
-
IMPORTANCE_DEFAULT
:(默認級別) -
IMPORTANCE_HIGH
:(高) -
IMPORTANCE_LOW
:(低) -
IMPORTANCE_MAX
:最重要的通知,系統(tǒng)會立馬使得消息顯示在屏幕
接下來通過NotificationChannel
創(chuàng)建一個渠道,構(gòu)造方法的三個參數(shù)分別是:Id、name、importance。
通過setDescription() 方法設(shè)置渠道的表述消息
最后在系統(tǒng)中注冊消息即可:
notificationManager.createNotificationChannel(notificationChannel);
1.2、擴充知識CharSequence
使用 CharSequence
類型的好處在于它是一個通用接口,它可以包含不同類型的字符序列,包括不可變的字符串 String
和可變的字符串 StringBuilder
、StringBuffer
等。這樣,我們在定義通知渠道時,可以使用 CharSequence
類型的變量,允許傳遞不同類型的字符序列。
1.3 創(chuàng)建通知
注意Android 8以上都需要獲取權(quán)限
//申請通知權(quán)限
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1);
}
還需要在AndroidManifest中進行注冊:
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
接下來才進行創(chuàng)建通知:
//創(chuàng)建通知
Notification notification = new NotificationCompat.Builder(MainActivity.this, "001")
.setContentTitle("QQ消息") //消息的標(biāo)題
.setContentText("你好,我是張三") //消息的內(nèi)容
.setWhen(System.currentTimeMillis()) //指定通知被創(chuàng)建的時間
.setSmallIcon(R.drawable.notify) //通知的小圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource
(getResources(), R.drawable.notify)) //通知的大圖標(biāo)
.build();
//顯示一個通知
notificationManager.notify(1, notification);
在 setLargeIcon()
方法中,需要傳遞一個 Bitmap
對象作為大圖標(biāo)的內(nèi)容。這里使用了 BitmapFactory.decodeResource(getResources(), R.drawable.notify)
來將 R.drawable.notify
資源轉(zhuǎn)換成 Bitmap
對象,并將其作為大圖標(biāo)設(shè)置給通知。
1.4 為消息設(shè)置點擊事件
//點擊通知后跳轉(zhuǎn)頁面
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
使用PendingIntent
,他有四個參數(shù):
- 第二個參數(shù)一般不會使用,通常傳入0
- 第三個參數(shù)是Intent對象
- 第四個參數(shù)是一個標(biāo)志(flag),用于確定意圖的行為
有四個值可選通常是FLAG_IMMUTABLE
-
PendingIntent.FLAG_IMMUTABLE: 這是 Android 12(API 級別 31)及更高版本中引入的標(biāo)志。創(chuàng)建的
PendingIntent
對象將變?yōu)椴豢勺儭?strong>不可變的PendingIntent
在創(chuàng)建后就無法修改其內(nèi)容、標(biāo)志或其他屬性,提高了安全性和性能。 -
PendingIntent.FLAG_UPDATE_CURRENT: 用于指定如果創(chuàng)建的
PendingIntent
已經(jīng)存在,那么使用現(xiàn)有的PendingIntent
,并更新其中的Intent
內(nèi)容為新傳入的Intent
。如果不存在,就創(chuàng)建一個新的PendingIntent
。這個標(biāo)志通常用于在多次創(chuàng)建相同PendingIntent
的場景,確保只有一個PendingIntent
實例,并且其中的Intent
內(nèi)容保持最新。 -
PendingIntent.FLAG_CANCEL_CURRENT:如果創(chuàng)建的
PendingIntent
已經(jīng)存在,那么將現(xiàn)有的PendingIntent
取消掉,然后創(chuàng)建一個新的PendingIntent
。即先取消已存在的PendingIntent
,再創(chuàng)建新的。 -
PendingIntent.FLAG_NO_CREATE:如果創(chuàng)建的
PendingIntent
已經(jīng)存在,不會再創(chuàng)建新的PendingIntent
,而是返回已存在的PendingIntent
。如果不存在,返回null
。這個標(biāo)志通常用于查詢是否已經(jīng)存在特定的PendingIntent
,而不會真正創(chuàng)建新的實例。
最后使用在創(chuàng)建通知時加上:
.setContentIntent(pi) //點擊后的跳轉(zhuǎn)事件
然后點擊這個通知就可以實現(xiàn)跳轉(zhuǎn)了。
通過這個動圖,我們發(fā)現(xiàn)點擊通知后通知依然沒有消失,這是為什么?
解決方法有兩種:
- 一種是在
NotificationCompat.Builder
中再連綴一個**setAutoCancel()**方法
- 另一種是顯式的調(diào)用
NotificationManager
中的**cancel()**方法。
這個1就是我們在創(chuàng)建通知時,為這個通知設(shè)置的id
2、通知的進階用法
android 13都需要在渠道中注冊
2.1 設(shè)置震動
//注冊震動
long[] vibrationPattern = {100, 200, 300, 400}; // 設(shè)置震動模式,參數(shù)為一個 long 類型數(shù)組,表示震動的時長和間隔
// 配置通知出現(xiàn)時的震動(如果 Android 設(shè)備支持的話)
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(vibrationPattern);
notificationChannel.setVibrationPattern(vibrationPattern);這段代碼也可以不在渠道中設(shè)置,只需要使用notificationChannel.enableVibration(true);注冊。
然后在通知NotificationCompat.Builde
r中:
.setVibrate(new long[]{100, 200, 300, 400})
注意設(shè)置權(quán)限;
<uses-permission android:name="android.permission.VIBRATE"/>
2.2 設(shè)置閃爍燈
// 設(shè)置通知渠道的閃燈效果
notificationChannel.enableLights(true); // 允許通知閃燈
然后在通知NotificationCompat.Builde
r中:
.setLights(Color.RED,1000,2000)
第一個參數(shù)是顏色,第二個是亮燈時長,第三個是暗燈時長文章來源:http://www.zghlxwxcb.cn/news/detail-798104.html
2.3 設(shè)置聲音
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Big_Easy.ogg")))
2.4 創(chuàng)建富文本通知內(nèi)容
.setStyle(new NotificationCompat.BigTextStyle().bigText("ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss"))
文章來源地址http://www.zghlxwxcb.cn/news/detail-798104.html
2.5 顯示大圖
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.notify)))
到了這里,關(guān)于【Android】最新版Android13使用Notification,Notification的基本使用和進階使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!