Android 8.0实现发送通知

 更新时间:2020年07月29日 11:01:15   作者:爱码士_yan  
这篇文章主要为大家详细介绍了Android 8.0实现发送通知,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android使用WebView实现文件下载功能

    Android使用WebView实现文件下载功能

    这篇文章主要为大家详细介绍了Android使用WebView实现文件下载功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Android实现拍照、选择图片并裁剪图片功能

    Android实现拍照、选择图片并裁剪图片功能

    这篇文章主要为大家详细介绍了Android实现拍照、选择图片并裁剪图片功能的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • Android实现二维码扫描并登陆网页

    Android实现二维码扫描并登陆网页

    这篇文章主要介绍了Android实现二维码扫描并登陆网页的相关资料,需要的朋友可以参考下
    2016-05-05
  • Android Shader应用开发之雷达扫描效果

    Android Shader应用开发之雷达扫描效果

    这篇文章主要为大家详细介绍了Android Shader应用开发之雷达扫描效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • 读写Android中assets目录下的文件的方法详解

    读写Android中assets目录下的文件的方法详解

    这篇文章主要介绍了读写Android中assets目录下的文件的方法详解,assets和res/raw工程目录下都可以放一些文件,这些文件将被打包到APK中应用使用,需要的朋友可以参考下
    2016-04-04
  • Android 启动模式FLAG_ACTIVITY_CLEAR_TOP案例详解

    Android 启动模式FLAG_ACTIVITY_CLEAR_TOP案例详解

    这篇文章主要介绍了Android 启动模式FLAG_ACTIVITY_CLEAR_TOP案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Flutter 给列表增加下拉刷新和上滑加载更多功能

    Flutter 给列表增加下拉刷新和上滑加载更多功能

    在实际的 App 中,下拉刷新和上滑加载更多是非常常见的交互形式。在 Flutter 中,有 flutter_easyrefresh开源插件用于实现下拉刷新和上滑加载更多。本篇介绍了有状态组件和 flutter_easyrefresh 的基本应用,同时使用模拟的方式完成了异步数据加载。
    2021-05-05
  • Android自定义View制作仪表盘界面

    Android自定义View制作仪表盘界面

    这篇文章主要介绍了Android自定义View制作仪表盘界面的相关资料,首先需要自定义仪表盘的属性,在构造方法种获取自定义属性,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下
    2016-11-11
  • Android自定义textview实现竖直滚动跑马灯效果

    Android自定义textview实现竖直滚动跑马灯效果

    这篇文章主要为大家详细介绍了Android自定义textview实现竖直滚动跑马灯效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Android ContentProvider实现获取手机联系人功能

    Android ContentProvider实现获取手机联系人功能

    这篇文章主要为大家详细介绍了Android ContentProvider实现获取手机联系人功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07

最新评论