国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

Android 8.0實現發送通知

瀏覽:11日期: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
相關文章:
主站蜘蛛池模板: 农村寡妇一级毛片免费看视频 | 久草在线免费福利视频 | 国产日本韩国不卡在线视频 | 亚洲国产精品67194成人 | 久久黄色免费网站 | 在线观看国产一区二三区 | 亚洲精品午夜国产va久久成人 | 日本aaa毛片 | 在线午夜影院 | 日韩欧美一区二区三区在线 | 欧美日韩精品高清一区二区 | 玖玖影院在线观看 | 亚洲高清视频网站 | 日韩制服诱惑 | 国产精品手机在线播放 | 大臿蕉香蕉大视频成人 | 草草在线观看视频 | 黄+色+性+人免费 | 亚洲综合中文 | 国产成人精品曰本亚洲77美色 | 精品无码久久久久久国产 | 中文字幕在线观看日韩 | 国产精品久久久影院 | 日本午夜高清视频 | 日韩三级在线观看 | 精品国产97在线观看 | 欧美亚洲日本视频 | 成人免费一级片 | 夜色精品国产一区二区 | 一区二区网站在线观看 | 国产成人精品曰本亚洲77美色 | 国产成人深夜福利在线观看 | 波多野结衣aⅴ在线 | 国产亚洲人成a在线v网站 | 国产日韩在线观看视频 | jizz国产精品免费麻豆 | 美女毛片在线看 | 三级黄色免费网站 | 韩国三级日本三级香港三级黄 | 在线观看 a国v | 一级aaaaaa毛片免费 |