色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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
相關文章:
主站蜘蛛池模板: 亚洲图片 自拍偷拍 | 日韩欧美视频在线播放 | 国产欧美久久久精品 | 91香蕉嫩草| 怡红院色视频在线 | 青青操在线视频 | 久久精品免费全国观看国产 | 国产精品亚洲成在人线 | 国产天堂亚洲精品 | 最新版天堂资源中文官网 | 岛国大片在线播放免费 | 国产一区私人高清影院 | 日本三片在在线播放 | 武松金莲肉体交战在线观看 | 国产亚洲精品91 | 欧美在线乱妇一级毛片 | 国产成人香蕉久久久久 | f性欧美| 真人一级毛片全部免 | 欧美日韩日本国产 | 九草视频在线观看 | 国产精品美女一区二区三区 | 久久久精品2018免费观看 | 精品成人在线 | 成人精品一区二区激情 | 男女免费在线视频 | 亚洲美女视频网址 | 国产高清三级 | 日本久草视频 | 亚洲国产精品影院 | 国产一级在线 | 精品玖玖玖视频在线观看 | 热热涩热热狠狠色香蕉综合 | 成人在线免费播放 | 国产成人精品在线观看 | 国产亚洲一欧美一区二区三区 | 亚洲欧美成人 | 国产成人在线小视频 | 成人永久福利在线观看不卡 | 一区二区中文字幕亚洲精品 | 亚洲综合久久久久久888 |