Android studio 4.0.1    SDK 30 Android10.0+

Android 8.0之后需要配置通知渠道 来看一下官方文档的实例代码

https://developer.android.com/training/notify-user/build-notification#java

    private void createNotificationChannel() {        // Create the NotificationChannel, but only on API 26+ because        // the NotificationChannel class is new and not in the support library        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            CharSequence name = getString(R.string.channel_name);            String description = getString(R.string.channel_description);            int importance = NotificationManager.IMPORTANCE_DEFAULT;            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);            channel.setDescription(description);            // Register the channel with the system; you can't change the importance            // or other notification behaviors after this            NotificationManager notificationManager = getSystemService(NotificationManager.class);            notificationManager.createNotificationChannel(channel);        }    }    

首先需要配置通知渠道,new NotificationChannel(CHANNEL_ID, name, importance); 实例化渠道对象时配置渠道ID,名字和描述

ID不在app中显性显示,Message ,app message 分别为配置的渠道名和渠道描述,可以在application中直接对需要申请的渠道进行申请, 官方文档中亦有描述解释渠道申请代码可以反复被执行,创建现有通知渠道不会执行任何操作

渠道创建好之后下面构建一个通知进行提示

    // Create an explicit intent for an Activity in your app    Intent intent = new Intent(this, AlertDetails.class);    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)            .setSmallIcon(R.drawable.notification_icon)            .setContentTitle("My notification")            .setContentText("Hello World!")            .setPriority(NotificationCompat.PRIORITY_DEFAULT)            // Set the intent that will fire when the user taps the notification            .setContentIntent(pendingIntent)            .setAutoCancel(true);    

其中 AlertDetails.class 为点击通知进行执行的意图指向方,setFlags部分参考官方文档说明,按需配置

在合适的地方触发发送通知

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);    // notificationId is a unique int for each notification that you must define    notificationManager.notify(notificationId, builder.build());    

注意此处需配置每个种类唯一ID 例如

notificationManager.notify(0, builder.build());

如果需要显示悬浮通知,在申请渠道时务必将优先级设置为

int importance = NotificationManager.IMPORTANCE_HIGH;

 

更多相关文章

  1. Android(安卓)全屏
  2. Android(安卓)Bad notification for startForeground: java.lang
  3. android需要的访问权限(不断更新,希望大家多交流)
  4. Android(安卓)解决Handler在运行时加载报空指针异常
  5. Android(安卓)Bluetooth 文件接收路径修改方法
  6. listview 左滑弹出删除按钮,需要折腾一下
  7. Android(安卓)Studio(十二):打包多个发布渠道的apk文件
  8. Android(安卓)判断通知栏权限的问题
  9. Activity从入门到放弃

随机推荐

  1. android xml常规布局属性
  2. android国际化操作
  3. Android(安卓)编程下 Managing Your App'
  4. Android(安卓)五大布局
  5. react-native 热更新(android)
  6. Android图形层叠 – Layer-list
  7. 我的android 第4天 - Dialog
  8. Android(安卓)网络(一) HTTP协议
  9. android Intent机制详解
  10. Android(安卓)- 文件读写操作 总结