欢迎来到代码驿站!

当前位置:首页 >

Android 8.0实现发送通知

时间:2020-07-29 12:01:06|栏目:|点击:

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

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());
 }

上一篇:Ubuntu18.04安装Nvidia显卡驱动教程(图文)

栏    目:

下一篇:R语言ggplot2边框背景去除的实现

本文标题:Android 8.0实现发送通知

本文地址:http://www.codeinn.net/misctech/2790.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有