NotificationManager: notifyAsUser: tag=null, id=6, user=UserHandle{0} 广播推送Notification不成功 安卓不兼容问题

在使用安卓8.0版本以上手机测试广播通知时,会出现通知不显示的bug

测试广播推送消息的代码时,在电脑的模拟器上可以实现,但是连接到安卓手机的时候就失败,查阅网上的资料,可以发现时安卓版本不兼容的问题。

我用的是bmob云数据库,直接用官方文档中的方法,添加抽象类MyPushMessageReceiver实现广播推送通知,后台推送消息的时候显示并未推送到手机

在这里插入图片描述

解决办法:解决安卓兼容问题

  if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
                    //builder的channelId需和下面channel的保持一致;
                    builder.setChannelId("channel_id");
                    NotificationChannel channel = new
                            NotificationChannel("channel_id","channel_name",
                            NotificationManager.IMPORTANCE_DEFAULT);
                    channel.setBypassDnd(true);//设置可以绕过请勿打扰模式
                    channel.canBypassDnd();//可否绕过请勿打扰模式
                    //锁屏显示通知
                    channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
                    channel.shouldShowLights();//是否会闪光
                    channel.enableLights(true);//闪光
                    //指定闪光时的灯光颜色,为了兼容低版本在上面builder上通过setLights方法设置了
                    //channel.setLightColor(Color.RED);
                    channel.canShowBadge();//桌面launcher消息角标
                    channel.enableVibration(true);//是否允许震动
                    //震动模式,第一次100ms,第二次100ms,第三次200ms,为了兼容低版本在上面builder上设置了
                    //channel.setVibrationPattern(new long[]{100,100,200});
                    channel.getAudioAttributes();//获取系统通知响铃声音的配置
                    channel.getGroup();//获取通知渠道组
                    //绑定通知渠道
                    manager.createNotificationChannel(channel);
                }

上述代码使用了NotificationChannel(通知渠道)的概念,用来允许您为要显示的每种通知类型创建用户可自定义的渠道。用户界面将通知渠道称之为通知类别。

这里贴出我的MyPushMessageReceiver广播类的代码,主体是重写的bmob开发文档中原有的广播推送代码

public class MyPushMessageReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
//        if(intent.getAction().equals(PushConstants.ACTION_MESSAGE)){
//            Log.d("bmob", "客户端收到推送内容:"+intent.getStringExtra("msg"));
//        }
        if (intent.getAction().equals(PushConstants.ACTION_MESSAGE)) {
            // 收到广播时,发送一个通知
            String jsonStr = intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING);
            String content = null;
            try {
                // 处理JSON
                JSONObject jsonObject = new JSONObject(jsonStr);
                content = jsonObject.getString("alert");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //获取PendingIntent
            Intent mainIntent = new Intent(context, SchoolBabyActivity.class);//此处设置点击跳转的界面
            PendingIntent mainPendingIntent = PendingIntent.getActivity(context, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);

            Notification.Builder builder = new Notification.Builder(context);
            //Notification notify = new Notification.Builder(context);//设置点击内容的点击意图
            builder.setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true)//设置点击后自动清除
                    .setContentTitle("来自益起的通知")
                    .setTicker("提示消息来啦!")
                    .setWhen(System.currentTimeMillis())//设置状态栏里的通知时间
                    .setContentText(content)//设置通知栏中的文本内容
                    .setContentIntent(mainPendingIntent)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),R.drawable.logo));

            if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
                //builder的channelId需和下面channel的保持一致;
                builder.setChannelId("channel_id");
                NotificationChannel channel = new
                        NotificationChannel("channel_id","channel_name",
                        NotificationManager.IMPORTANCE_DEFAULT);
                channel.setBypassDnd(true);//设置可以绕过请勿打扰模式
                channel.canBypassDnd();//可否绕过请勿打扰模式
                //锁屏显示通知
                channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
                channel.shouldShowLights();//是否会闪光
                channel.enableLights(true);//闪光
                //指定闪光时的灯光颜色,为了兼容低版本在上面builder上通过setLights方法设置了
                //channel.setLightColor(Color.RED);
                channel.canShowBadge();//桌面launcher消息角标
                channel.enableVibration(true);//是否允许震动
                //震动模式,第一次100ms,第二次100ms,第三次200ms,为了兼容低版本在上面builder上设置了
                //channel.setVibrationPattern(new long[]{100,100,200});
                channel.getAudioAttributes();//获取系统通知响铃声音的配置
                channel.getGroup();//获取通知渠道组
                //绑定通知渠道
                manager.createNotificationChannel(channel);
            }

            Notification notify1=builder.build();
            manager.notify(R.string.app_name, notify1);

        }


    }

}

同时还可以点击通知消息进入对应的程序界面
最后实现效果如下图

在这里插入图片描述

最后成功,这就很奶思了,具体的各项文字图片修改代码可自行查阅。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值