【问题标题】:Android O reporting notification not posted to channel - but it isAndroid O 报告通知未发布到频道 - 但它是
【发布时间】:2017-06-11 23:29:30
【问题描述】:

几个 Android O 通知问题:

1) 我创建了一个通知通道(见下文),正在使用 .setChannelId() 调用构建器(传入我创建的通道的名称“wakey”;但是,当我运行应用程序时,我收到一条消息,说我未能向频道“null”发布通知。这可能是什么原因?

2) 我怀疑#1 的答案可以在它说要检查的“日志”中找到,但我已经检查了 logcat 并且没有看到任何关于通知或频道的信息。它说要查看的日志在哪里?

这是我用来创建频道的代码:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence name = context.getString(R.string.app_name);
String description = "yadda yadda"
int importance = NotificationManager.IMPORTANCE_DEFAULT;

NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, name, importance);
channel.setDescription(description);

notificationManager.createNotificationChannel(channel);

这是生成通知的代码:

Notification.Builder notificationBuilder;

Intent notificationIntent = new Intent(context, BulbActivity.class);
notificationIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // Fix for https://code.google.com/p/android/issues/detail?id=53313

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

Intent serviceIntent = new Intent(context, RemoteViewToggleService.class);
serviceIntent.putExtra(WakeyService.KEY_REQUEST_SOURCE, WakeyService.REQUEST_SOURCE_NOTIFICATION);

PendingIntent actionPendingIntent = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
_toggleAction = new Notification.Action(R.drawable.ic_power_settings_new_black_24dp, context.getString(R.string.toggle_wakey), actionPendingIntent);

notificationBuilder= new Notification.Builder(context)
    .setContentTitle(context.getString(R.string.app_name))
    .setContentIntent(contentIntent)
    .addAction(_toggleAction);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    notificationBuilder.setChannelId(NOTIFICATION_CHANNEL);
}

notificationBuilder.setSmallIcon(icon);
notificationBuilder.setContentText(contentText);
_toggleAction.title = actionText;

int priority = getNotificationPriority(context);
notificationBuilder.setPriority(priority);
notificationBuilder.setOngoing(true);

Notification notification = notificationBuilder.build();
notificationManager.notify(NOTIFICATION_ID, notification);

这是我收到的警告:

【问题讨论】:

    标签: android notifications android-8.0-oreo


    【解决方案1】:

    我想我已经学到了一些东西,这些东西加起来就是一个答案:

    1. 我使用的是模拟器设备,其中的图片不包含 Play 商店。
    2. 图片上的 Google Play 服务版本不是最新的,所以我应该收到通知,告诉我需要升级。由于该通知未应用于频道,因此未显示。
    3. 如果我在 Android Studio 中将 logcat 设置为“无过滤器”而不是“仅显示选定的应用程序”,那么我发现日志指出有问题的通知是 Play Services“需要更新”通知。

    所以,我换了一张包含 Play 商店的图片,它正确显示了通知(也许该通知的频道是由 Play 商店设置的?),让我更新到最新的 Google Play 服务,从那以后我就再也没有看到过这个警告。

    所以,长话短说(为时已晚) - 对于 Android O,如果您在模拟器上使用 Google Play 服务和测试,请选择包含 Play 商店的图像,或者忽略 toast(祝您好运! )。

    【讨论】:

    • 尝试下面的代码来设置频道: new Notification.Builder(getApplicationContext(),FOLLOWERS_CHANNEL) .setContentTitle(title) .setContentText(body) .setSmallIcon(getSmallIcon());
    【解决方案2】:

    我也遇到了同样的问题,用构造函数解决了

    new Notification.Builder(Context context, String channelId),而不是deprecated onAPI 级别 >=26 (Android O): new NotificationCompat.Builder(Context context)

    如果您的 notificationBuilder 是使用已弃用的构造函数构建的,则以下代码将不起作用:

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    notificationBuilder.setChannelId(NOTIFICATION_CHANNEL);}
    

    【讨论】:

    • 由于这个弃用的构造函数,我有大约 1k 次崩溃...谢谢!
    【解决方案3】:

    首先创建通知通道:

     public static final String NOTIFICATION_CHANNEL_ID = "4565";
    //Notification Channel
            CharSequence channelName = NOTIFICATION_CHANNEL_NAME;
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
    
    
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
    

    然后在构造函数中使用通道id:

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setSmallIcon(R.drawable.ic_timers)
                    .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                    .setSound(null)
                    .setChannelId(NOTIFICATION_CHANNEL_ID)
                    .setContent(contentView)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setLargeIcon(picture)
                    .setTicker(sTimer)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(false);
    

    【讨论】:

    • 为什么需要 setChannelId(NOTIFICATION_CHANNEL_ID)
    【解决方案4】:

    您必须先创建一个频道。

    private void createNotificationChannel() {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                CharSequence name = getString(R.string.channel_name);
                String description = getString(R.string.channel_description);
                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
                channel.setDescription(description);
                // Register the channel with the system; you can't change the importance
                // or other notification behaviors after this
                NotificationManager notificationManager = getSystemService(NotificationManager.class);
                notificationManager.createNotificationChannel(channel);
            }
    }
    
    public void notifyThis(String title, String message) {
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.green_circle)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    
            // notificationId is a unique int for each notification that you must define
            notificationManager.notify(0, mBuilder.build());
    }
    

    最后你调用了这个方法:

    createNotificationChannel();
    notifyThis("My notification", "Hello World!");
    

    【讨论】:

      【解决方案5】:

      使用以下代码创建通知:

          Notification notification = new Notification.Builder(MainActivity.this)
                    .setContentTitle("New Message")
                              .setContentText("You've received new messages.")
                              .setSmallIcon(R.mipmap.ic_launcher)
                              .setChannelId(channelId)
                              .build();
      

      不使用:

      Notification notification = new NotificationCompat.Builder(MainActivity.this)
                          .setContentTitle("Some Message")
                          .setContentText("You've received new messages!")
                          .setSmallIcon(R.mipmap.ic_launcher)
                          .setChannel(channelId)
                          .build();
      

      【讨论】:

        【解决方案6】:

        你必须先创建一个 NotificationChannel

        val notificationChannel = NotificationChannel("channelId", "channelName", NotificationManager.IMPORTANCE_DEFAULT)
        notificationManager.createNotificationChannel(notificationChannel);
        

        这是显示 API 26+ 通知的唯一方式

        【讨论】:

        • 原始问题指出我创建了一个通知渠道。也就是说,我解决了这个问题并在接受的答案中解释了所有内容。
        【解决方案7】:

        我也遇到了同样的问题。它通过创建 NotificationChannel 并使用通知管理器添加新创建的通道来解决。

        【讨论】:

        • 原始问题指出我创建了一个通知渠道。也就是说,我解决了这个问题并在接受的答案中解释了所有内容。
        【解决方案8】:

        我还想补充一点,如果您使用的是 Build tools v26+,您将收到此错误:

        app/build.grade

        compileSdkVersion 26
        buildToolsVersion "26.0.2"
        
        defaultConfig {
            targetSdkVersion 26
        

        降级到最低版本应该可以正常工作。

        【讨论】:

        • 这只是建议不要使用最新的 SDK 来避免该问题。这不是一个可接受的解决方案。
        猜你喜欢
        • 2018-03-02
        • 1970-01-01
        • 1970-01-01
        • 2020-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-16
        相关资源
        最近更新 更多