解决 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());
        }
    }

猜你喜欢

转载自blog.csdn.net/xiexiaotian11/article/details/90054467