av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Android 8.0實現發送通知

瀏覽:12日期: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.發送通知

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

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

標簽: Android
相關文章:
主站蜘蛛池模板: 国产精品久久久久久久久久久久久 | 国产精品免费播放 | 福利久久 | 国产欧美一区二区三区另类精品 | 国产精品夜夜夜一区二区三区尤 | 午夜精品一区二区三区在线视频 | av中文在线| 国产大学生情侣呻吟视频 | 久久毛片 | 国产精品久久国产精品久久 | 天天草草草 | 国产亚洲精品美女久久久久久久久久 | 日韩欧美在 | 国产一区二区av | 亚洲 日本 欧美 中文幕 | 欧美九九九 | 亚洲黄色av网站 | 日韩欧美在线视频 | 日韩中文字幕免费在线 | 毛片免费观看 | 久久国产精彩视频 | 欧美不卡视频 | 青青草免费在线视频 | 国产一区二区三区四区五区加勒比 | 免费av手机在线观看 | 欧美激情精品久久久久久 | 国产成人精品a视频一区www | 国产欧美精品一区二区 | 中文字幕日韩在线 | 精品国产不卡一区二区三区 | 成人福利片 | 91视在线国内在线播放酒店 | 激情a| 狠狠躁天天躁夜夜躁婷婷老牛影视 | 国产99久久精品一区二区永久免费 | 日本三级全黄三级三级三级口周 | 伊人狼人影院 | 欧美色欧美亚洲另类七区 | 亚洲国产一区二区三区, | 91精品国产综合久久香蕉922 | 精品视频一区在线 |