最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 8.0實(shí)現(xiàn)發(fā)送通知

 更新時(shí)間:2020年07月29日 11:01:15   作者:愛碼士_yan  
這篇文章主要為大家詳細(xì)介紹了Android 8.0實(shí)現(xiàn)發(fā)送通知,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在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 類

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 channel
        mediaChannel
    ));
  }
}

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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

尖扎县| 潞城市| 壶关县| 济南市| 阿克苏市| 那曲县| 镇江市| 宁国市| 沾益县| 科技| 永丰县| 巧家县| 博乐市| 霍林郭勒市| 闵行区| 海盐县| 门头沟区| 奈曼旗| 山阴县| 鄂温| 阳城县| 布尔津县| 双江| 昌黎县| 洛隆县| 大冶市| 喀喇沁旗| 屏东市| 堆龙德庆县| 青龙| 乡城县| 安仁县| 榆社县| 邵阳县| 平顺县| 惠州市| 布尔津县| 吉首市| 外汇| 平湖市| 弥勒县|