午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

Android 8.0實現(xiàn)發(fā)送通知

瀏覽:20日期:2022-09-23 09:55:03

在Android8.0以后,針對Notification 通知api做了修改,新增了通知渠道(NotificationCannel)。下面就把demo的詳細代碼記錄下:

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 channelmediaChannel )); }}

3.發(fā)送通知

public void sendSimpleNotification(Context context, NotificationManager nm) { //創(chuàng)建點擊通知時發(fā)送的廣播 Intent intent = new Intent(context, NotificationMonitorService.class); intent.setAction('android.service.notification.NotificationListenerService'); PendingIntent pi = PendingIntent.getService(context,0,intent,0); //創(chuàng)建刪除通知時發(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)//設置通知左側(cè)的小圖標.setSmallIcon(R.drawable.ic_notification)//設置通知標題.setContentTitle('Simple notification')//設置通知內(nèi)容.setContentText('Demo for simple notification!')//設置點擊通知后自動刪除通知.setAutoCancel(true)//設置顯示通知時間.setShowWhen(true)//設置通知右側(cè)的大圖標.setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))//設置點擊通知時的響應事件.setContentIntent(pi)//設置刪除通知時的響應事件.setDeleteIntent(deletePendingIntent); //發(fā)送通知 nm.notify(Notificaitons.NOTIFICATION_SAMPLE,nb.build()); }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Android
相關(guān)文章:
主站蜘蛛池模板: 午夜久久影院 | 欧美精品入口蜜桃 | 97久久精品人人澡人人爽 | 久久精品视频网站 | 欧美日韩a v| 久久国产成人 | 奇米网狠狠干 | 欧美伊人久久 | 成人网影 | 精品国产乱码久久久久久88av | 日本在线观看网址 | 特淫毛片 | 色狠狠综合 | 日日夜夜草 | 日韩精品久久久久 | 国产91在线播放九色 | 亚洲美女视频在线观看 | 色狠狠综合网 | 一区二区三区日韩视频 | 91午夜理伦私人影院 | 免费观看成人 | 欧美成人一区二区三区四区 | 午夜影视福利 | 在线观看成人 | av网址导航 | 亚洲精品三区 | 成人综合网站 | 最近中文字幕在线视频 | 久久久久少妇 | 亚洲激情四射 | 午夜久久网 | 成人午夜在线播放 | 亚洲天堂自拍 | 国产黄色一级片 | 激情视频久久 | 自拍视频在线观看 | 国产视频一区在线 | 羞羞免费视频 | 视频免费在线 | 看av片 | 久草综合在线 |