解决 No Channel found for pkg=

 下面这段代码是我的  通知代码,我主要的问题是之前用的废弃的方法不起作用了,

NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(getApplicationContext())

 

现在修改如下

 Notification notification = new NotificationCompat.Builder(getApplicationContext(),channelId)

 

 

  /**
     * 显示到通知栏
     *
     * @param messageBodyAttr
     */
    private void showNotice(MessageBodyAttr messageBodyAttr,  String channelId) {
        String title = messageBodyAttr.getTitle();
        String content = messageBodyAttr.getMessageContent();
        Intent intent = new Intent(MessageNotificationBroadcastReceiver.Companion.getACTION());
        intent.setComponent(new ComponentName(getPackageName(), MessageNotificationBroadcastReceiver.class.getName()));
        intent.putExtra("notificationId", 0);
        intent.putExtra("data", messageBodyAttr);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(DataHelper.context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
        remoteViews.setImageViewResource(R.id.icon, R.mipmap.default_head);
        remoteViews.setTextViewText(R.id.title, TextUtils.isEmpty(title) ? "通知" : title);
        remoteViews.setTextViewText(R.id.content, TextUtils.isEmpty(content) ? "" : content);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel notificationChannel =
                    new NotificationChannel(channelId, TextUtils.isEmpty(title) ? "通知" : title, IMPORTANCE_DEFAULT);
            notificationChannel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
            Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            notificationChannel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
            notificationChannel.enableLights(true);//闪光灯
            notificationChannel.setShowBadge(true);
            notificationChannel.enableVibration(true);//是否允许震动
            notificationManager.createNotificationChannel(notificationChannel);
            Notification notification = new NotificationCompat.Builder(getApplicationContext(),channelId)
                    .setSmallIcon(R.mipmap.default_head)
                    .setSound(soundUri)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setCustomContentView(remoteViews).build();
            notificationManager.notify(1,notification);
        } else {

            NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "1");
            builder.setDefaults(Notification.DEFAULT_ALL)
                    .setSmallIcon(R.mipmap.default_head)//设置小图标
                    .setContentTitle(messageBodyAttr.getTitle())
                    .setContentText(messageBodyAttr.getMessageContent())
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .build().flags = Notification.FLAG_AUTO_CANCEL;
            notificationManager.notify(0, builder.build());
        }
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值