分享

Android Notification

 wwwijhyt图书馆 2020-07-11

Android 通知不显示?

看书的时候,按照代码写一个通知,万万没想到,竟然显示不出来,我的天,My Cod !

手机坏了?  What ?

经过一份搜索,原来android 8.0之后通知需要加入channelId元素,否则无法显示通知,老衲手机是Android 9 (哼,趁我不注意自己升级,差评);

这就郁闷了,还是记录一下,下次遇到方便查找。


实现通知的示例代码(兼容Android O):

  1. /**
  2. * 显示通知
  3. */
  4. private void showFoldNotification() {
  5. NotificationCompat.Builder notificationCompatBuilder =
  6. new NotificationCompat.Builder(getApplicationContext(),
  7.   Objects.requireNonNull(createNotificationChannel(this)));
  8. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
  9. PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
  10. Notification notification = notificationCompatBuilder
  11. .setContentTitle("标题")
  12. .setContentText("内容")
  13. .setSmallIcon(R.mipmap.ic_launcher)
  14. .setLargeIcon(BitmapFactory.decodeResource(
  15. getResources(),
  16. R.mipmap.ic_launcher))
  17. .setContentIntent(pi)
  18. .setDefaults(NotificationCompat.DEFAULT_ALL)
  19. .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
  20.   .setWhen(System.currentTimeMillis())
  21. .setPriority(NotificationCompat.PRIORITY_DEFAULT)
  22. .setAutoCancel(true)
  23. .build();
  24. NotificationManagerCompat.from(getApplicationContext()).notify(1, notification);
  25. }

代码段 小部件

  1. /**
  2. * 删除通知渠道
  3. */
  4. NotificationChannel mChannel = mNotificationManager.getNotificationChannel(id);
  5. mNotificationManager.deleteNotificationChannel(mChannel);



详谈通知的基本使用:

创建通知:

  1. 获取NotificationManagers实例,管理通知:
  2. NotificationManager notificationManager =
  3. (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  4. 创建NotificationCompat.Builder对象
  5. NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channelId);

为Notification设置内容:

  1. .setContentTitle("标题") //设置通知标题
  2. .setContentText("内容") //设置通知内容
  3. .setSmallIcon(R.mipmap.ic_launcher) //设置通知的小图标
  4. .setLargeIcon(BitmapFactory.decodeResource(
  5. getResources(),
  6. R.mipmap.ic_launcher)) //设置通知图标
  7. .setWhen(System.currentTimeMillis()) //设置时间
  8. .setContentIntent(pi) // 设置点击通知时的意图
  9. .setDefaults(NotificationCompat.DEFAULT_ALL) //打开呼吸灯,声音,震动(下方详解)
  10. .setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary)) //设置通知栏颜色
  11. .setCategory(Notification.CATEGORY_REMINDER) //设置通知类别
  12. .setPriority(NotificationCompat.PRIORITY_DEFAULT) //设置优先级,级别高的排在前面
  13. .setVibrate(new long[]{100,200,300,400}) //设置震动
  14. .setLight(Color.Red,500,500) //设置呼吸灯效果,一般三种颜色:红绿蓝
  15. .setAutoCancel(true); //设置通知点击后图标消失

 创建NotificationChannel :

public static String createNotificationChannel(Context context) {
  1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  2. channelId:全局唯一,长度不能过长
  3. channelName:渠道名称,显示在通知栏列表
  4. channelDescription: 设置描述 最长30字符
  5. importance:重要等级,等级不同在手机桌面上展示的顺序不同
  6. NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
  7. notificationChannel.setDescription(channelDescription);
  8. // 允许通知使用震动,默认为false
  9. notificationChannel.enableVibration(true);
  10. // 设置显示模式
  11. notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
  12. NotificationManager notificationManager =
  13. (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  14. notificationManager.createNotificationChannel(notificationChannel);
  15. return channelId;
  16. } else {
  17. return null;
  18. }
  19. }

删除渠道通知:

NotificationChannel mChannel = mNotificationManager.getNotificationChannel(id); mNotificationManager.deleteNotificationChannel(mChannel);


看书的时候,按照代码写一个通知,万万没想到,竟然显示不出来,我的天,手机坏了?  经过一份搜索,原来android 8.0之后通知需要加入channelId元素,否则无法显示通知,这就郁闷了,还是记录一下,下次遇到方便查找。

 

NotificationChannel 的方法列表:

getId() —  获取 ChannleId
enableLights() —  开启指示灯,如果设备有的话
setLightColor() —  设置指示灯颜色
enableVibration() —  开启震动
setVibrationPattern() —  设置震动频率
setImportance() —  设置频道重要性
getImportance() —  获取频道重要性
setSound() —  设置声音
getSound() —  获取声音
setGroup() —  设置 ChannleGroup
getGroup() —  得到 ChannleGroup
setBypassDnd() —  设置绕过免打扰模式
canBypassDnd() —  检测是否绕过免打扰模式
getName() —  获取名称
setLockScreenVisibility() —  设置是否应在锁定屏幕上显示此频道的通知
getLockscreenVisibility() —  检测是否应在锁定屏幕上显示此频道的通知
setShowBadge() 设置是否显示角标
canShowBadge() —  检测是否显示角标

 

 NotificationChannel中importance: 

IMPORTANCE_NONE = 0; 不提示,不展示

IMPORTANCE_MIN = 1; 不提示,在通知下拉栏会展示,但是是收起的

IMPORTANCE_LOW = 2; 会在状态栏中显示,但不会弹窗,通知下拉栏会展示

IMPORTANCE_DEFAULT = 3; 会在状态栏中显示,允许有声音提示,但不会弹窗,通知下拉栏会展示

IMPORTANCE_HIGH = 4; 会弹窗提示,允许有提示音

IMPORTANCE_MAX = 5; 会弹窗提示,允许有提示音,可以使用全屏

 

Notification.Builder中的setDefaults:

setDefaults(NotificationCompat.DEFAULT_VIBRATE) ;    // 添加默认震动提醒

setDefaults(NotificationCompat.DEFAULT_SOUND) ;     // 添加默认声音提醒

setDefaults(NotificationCompat.DEFAULT_LIGHTS) ;     // 添加默认闪光灯提醒

setDefaults(NotificationCompat.DEFAULT_ALL) ;           // 添加默认以上三种提醒


 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多