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

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

Android 8.0實現發送通知

瀏覽:3日期: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
相關文章:
主站蜘蛛池模板: 九九色综合网 | 国产理论视频 | 国产高清一国产免费软件 | 九九在线观看精品视频6 | 18年大片免费在线 | 99re最新这里只有精品 | 亚洲不卡视频在线观看 | 精品成人久久 | 国产精品白浆流出视频 | 手机看片精品高清国产日韩 | 可以看的毛片网站 | 亚洲精品综合久久中文字幕 | 国产精品毛片在线更新 | 成 人 黄 色 大 片 | 欧美综合一区二区三区 | 九九热久久免费视频 | 久久精品国产亚洲网址 | 欧美日韩在线视频 | 全部在线美女网站免费观看 | 美女va| 在线亚洲精品中文字幕美乳 | 国产精品情侣久久婷婷文字 | 97视频在线观看免费播放 | 99久久精品费精品国产一区二 | 国产成人精品实拍在线 | 国内精品九一在线播放 | 欧美手机在线视频 | 成人偷拍视频 | 香港台湾经典三级a视频 | 色一伦一情一区二区三区 | 午夜限制r级噜噜片一区二区 | 男人看片网址 | 97视频在线免费播放 | 亚洲国内精品 | 一区二区三区在线观看免费 | 亚洲国产精 | 中文字幕在线免费观看视频 | 在线不卡一区二区三区日韩 | 精品久久在线观看 | 国产午夜亚洲精品一区网站 | 国产成人www免费人成看片 |