Android 8.0實(shí)現(xiàn)發(fā)送通知
在Android8.0以后,針對(duì)Notification 通知api做了修改,新增了通知渠道(NotificationCannel)。下面就把demo的詳細(xì)代碼記錄下:
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 類(lèi)
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)建點(diǎn)擊通知時(shí)發(fā)送的廣播 Intent intent = new Intent(context, NotificationMonitorService.class); intent.setAction('android.service.notification.NotificationListenerService'); PendingIntent pi = PendingIntent.getService(context,0,intent,0); //創(chuàng)建刪除通知時(shí)發(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)//設(shè)置通知左側(cè)的小圖標(biāo).setSmallIcon(R.drawable.ic_notification)//設(shè)置通知標(biāo)題.setContentTitle('Simple notification')//設(shè)置通知內(nèi)容.setContentText('Demo for simple notification!')//設(shè)置點(diǎn)擊通知后自動(dòng)刪除通知.setAutoCancel(true)//設(shè)置顯示通知時(shí)間.setShowWhen(true)//設(shè)置通知右側(cè)的大圖標(biāo).setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))//設(shè)置點(diǎn)擊通知時(shí)的響應(yīng)事件.setContentIntent(pi)//設(shè)置刪除通知時(shí)的響應(yīng)事件.setDeleteIntent(deletePendingIntent); //發(fā)送通知 nm.notify(Notificaitons.NOTIFICATION_SAMPLE,nb.build()); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何在.net6webapi中使用自動(dòng)依賴(lài)注入2. VUE使用ElementUI下拉框 @change事件數(shù)據(jù)不回顯問(wèn)題3. Python如何對(duì)XML 解析4. 5個(gè)HTML5的常用本地存儲(chǔ)方式詳解與介紹5. vue頁(yè)面設(shè)置滾動(dòng)失敗的完美解決方案(scrollTop一直為0)6. 使用JSP實(shí)現(xiàn)簡(jiǎn)單的用戶(hù)登錄注冊(cè)頁(yè)面示例代碼解析7. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢(xún)、排序、分頁(yè)8. Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新留言效果9. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(二)10. JSP中out對(duì)象的實(shí)例詳解
