美文网首页
2019-04-04 Android 通知/通知渠道/版本兼容问

2019-04-04 Android 通知/通知渠道/版本兼容问

作者: YuQiao0303 | 来源:发表于2019-04-04 15:08 被阅读0次

第一行代码(第二版)中关于通知的写法已经过时了,这里参考作者郭神本人的文章,重新总结,得此笔记
郭霖:一起来学习Android 8.0系统的通知栏适配吧
NotificationChannel 适配填坑指南 - 简书

发送通知前的准备工作:create notification channel

这段代码写在哪儿都行,只需在显示通知前调用.
先写一个方法:

    /**
     * 创建通知通道
     * @param channelId
     * @param channelName
     * @param importance
     */
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        //设置震动响铃等,高版本仅在此处设置有效,notification中设置无效
        channel.enableLights(true);
        channel.enableVibration(true);
        channel.setVibrationPattern(new long[]{100,200,300,400});//设置震动:静止0ms,震动1000ms,静止1000毫秒,震动1000毫秒,需要声明权限
        channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);//设置是否在锁屏中显示
        channel.setSound((Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) ,null);

        /*
        for fragment:  (NotificationManager) getActivity().getSystemService
        for activity:  (NotificationManager) getSystemService
         */
        NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

然后调用这个方法即可:

//创建通知通道
        //这部分代码可以写在任何位置,只需要保证在通知弹出之前调用就可以了
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            /*
            * 渠道ID可以随便定义,只要保证全局唯一性就可以。
            * 渠道名称是给用户看的,需要能够表达清楚这个渠道的用途。
            * 重要等级的不同则会决定通知的不同行为,当然这里只是初始状态下的重要等级,用户可以随时手动更改某个渠道的重要等级,App是无法干预的。
            * */
            String channelId = "remind";
            String channelName = "排尿提醒";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);

            channelId = "other";
            channelName = "其他消息";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotificationChannel(channelId, channelName, importance);

发送通知

这里需要注意一定要有smallIcon,不然会报错

java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(channel=remind pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE)

    /**
     * 发送通知的代码
     */
    public void sendNotification() {
        NotificationManager manager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this.getContext(), "remind")  //注意了这里需要一个channelId
                .setContentTitle("宝宝尿了~")
                .setContentText("可以更换纸尿裤了哦~")
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .build();
        manager.notify(1, notification);
    }

修改铃声和震动的坑

  1. 这些只能在channel中设置,单独在notification中设置无效
  2. 设置后基本不能改,要改请参考:
    NotificationChannel 适配填坑指南 - 简书
    Android8.0 NotificationChannel修改铃声和振动的坑 - ..._CSDN博客

综上,不如直接弃用在notification 中设置震动响铃的方式,而是考虑通过其他方式实现。

下面这篇还讲了如何调用preference 中的设置
如何在Android中设置铃声+震动 - ruanjianxiong的专栏 - CSDN博客

android 情景模式之响铃+震动获取方法
https://blog.csdn.net/myhc2014/article/details/76982416

相关文章

网友评论

      本文标题:2019-04-04 Android 通知/通知渠道/版本兼容问

      本文链接:https://www.haomeiwen.com/subject/acofiqtx.html