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

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

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

在Android8.0以后,針對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)擊通知時發(fā)送的廣播
    Intent intent = new Intent(context, NotificationMonitorService.class);
    intent.setAction("android.service.notification.NotificationListenerService");
    PendingIntent pi = PendingIntent.getService(context,0,intent,0);
    //創(chuàng)建刪除通知時發(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)擊通知后自動刪除通知
        .setAutoCancel(true)
        //設(shè)置顯示通知時間
        .setShowWhen(true)
        //設(shè)置通知右側(cè)的大圖標(biāo)
        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_notifiation_big))
        //設(shè)置點(diǎn)擊通知時的響應(yīng)事件
        .setContentIntent(pi)
        //設(shè)置刪除通知時的響應(yīng)事件
        .setDeleteIntent(deletePendingIntent);
    //發(fā)送通知
    nm.notify(Notificaitons.NOTIFICATION_SAMPLE,nb.build());
 }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

县级市| 乌恰县| 孟州市| 龙泉市| 宁晋县| 和田县| 苍南县| 濉溪县| 西和县| 马鞍山市| 乌拉特中旗| 阜康市| 和龙市| 兰西县| 鲁山县| 日喀则市| 武穴市| 金华市| 巴青县| 漠河县| 板桥市| 西畴县| 金昌市| 宁强县| 诸暨市| 阆中市| 武穴市| 尼勒克县| 富源县| 庆元县| 遵化市| 庄浪县| 正阳县| 江源县| 镇康县| 驻马店市| 长葛市| 玛沁县| 尚志市| 兴业县| 读书|