Android 8.0實(shí)現(xiàn)發(fā)送通知
在Android8.0以后,針對(duì)Notification 通知api做了修改,新增了通知渠道(NotificationCannel)。下面就把demo的詳細(xì)代碼記錄下:
1.Application 為NotificationManager添加通知頻道
import android.app.Application; import com.xinrui.ndkapp.notification.NotificationChannels; public class NdkApplication extends Application { @Override public void onCreate() { super.onCreate(); NotificationChannels.createAllNotificationChannels(this); } }
2.NotificationChannels 類
public class NotificationChannels { public final static String CRITICAL = "critical"; public final static String IMPORTANCE = "importance"; public final static String DEFAULT = "default"; public final static String LOW = "low"; public final static String MEDIA = "media"; public static void createAllNotificationChannels(Context context) { NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); if(nm == null) { return; } NotificationChannel mediaChannel = new NotificationChannel( MEDIA, context.getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT); mediaChannel.setSound(null,null); mediaChannel.setVibrationPattern(null); nm.createNotificationChannels(Arrays.asList( new NotificationChannel( CRITICAL, context.getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH), new NotificationChannel( IMPORTANCE, context.getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT), new NotificationChannel( DEFAULT, context.getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW), new NotificationChannel( LOW, context.getString(R.string.app_name), NotificationManager.IMPORTANCE_MIN), //custom notification channel mediaChannel )); } }
3.發(fā)送通知
public void sendSimpleNotification(Context context, NotificationManager nm) { //創(chuàng)建點(diǎn)擊通知時(shí)發(fā)送的廣播 Intent intent = new Intent(context, NotificationMonitorService.class); intent.setAction("android.service.notification.NotificationListenerService"); PendingIntent pi = PendingIntent.getService(context,0,intent,0); //創(chuàng)建刪除通知時(shí)發(fā)送的廣播 Intent deleteIntent = new Intent(context,NotificationMonitorService.class); deleteIntent.setAction(Intent.ACTION_DELETE); PendingIntent deletePendingIntent = PendingIntent.getService(context,0,deleteIntent,0); //創(chuàng)建通知 Notification.Builder nb = new Notification.Builder(context, NotificationChannels.DEFAULT) //設(shè)置通知左側(cè)的小圖標(biāo) .setSmallIcon(R.drawable.ic_notification) //設(shè)置通知標(biāo)題 .setContentTitle("Simple notification") //設(shè)置通知內(nèi)容 .setContentText("Demo for simple notification!") //設(shè)置點(diǎn)擊通知后自動(dòng)刪除通知 .setAutoCancel(true) //設(shè)置顯示通知時(shí)間 .setShowWhen(true) //設(shè)置通知右側(cè)的大圖標(biāo) .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big)) //設(shè)置點(diǎn)擊通知時(shí)的響應(yīng)事件 .setContentIntent(pi) //設(shè)置刪除通知時(shí)的響應(yīng)事件 .setDeleteIntent(deletePendingIntent); //發(fā)送通知 nm.notify(Notificaitons.NOTIFICATION_SAMPLE,nb.build()); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 可拖動(dòng)的seekbar自定義進(jìn)度值
這篇文章主要介紹了Android 可拖動(dòng)的seekbar自定義進(jìn)度值的相關(guān)資料,有需要的朋友參考下2016-04-04Android開(kāi)發(fā)Compose remember原理解析
這篇文章主要為大家介紹了Android開(kāi)發(fā)Compose remember原理解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Android app會(huì)crash的原因及解決方法
這篇文章主要介紹了Android app會(huì)crash的原因及解決方法,幫助大家更好的進(jìn)行Android開(kāi)發(fā),感興趣的朋友可以了解下2020-12-12Android開(kāi)發(fā)ThreadPoolExecutor與自定義線程池詳解
這篇文章主要為大家介紹了Android開(kāi)發(fā)ThreadPoolExecutor與自定義線程池詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Android畫(huà)板開(kāi)發(fā)之橡皮擦功能
這篇文章主要為大家詳細(xì)介紹了Android畫(huà)板開(kāi)發(fā)之橡皮擦功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android中使用AsyncTask做下載進(jìn)度條實(shí)例代碼
這篇文章主要介紹了Android中使用AsyncTask做下載進(jìn)度條實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以參考下2017-01-01JetpackCompose Navigation導(dǎo)航實(shí)現(xiàn)流程
Navigation是Jetpack用于Android導(dǎo)航的組件,作用是處理頁(yè)面跳轉(zhuǎn),以及頁(yè)面跳轉(zhuǎn)過(guò)程中的交互。使用Navigation,你就需要為每個(gè)頁(yè)面設(shè)定一條唯一路徑,它是一個(gè)String常量,形式是DeepLink的樣子,從一個(gè)頁(yè)面跳轉(zhuǎn)到另一個(gè)頁(yè)面,它通過(guò)輸入目的地的路徑進(jìn)行轉(zhuǎn)跳2023-01-01