Android O报告通知未发布到频道 - 但确实如此

jka*_*001 41 notifications android android-8.0-oreo

几个Android O通知问题:

1)我创建了一个Notification Channel(见下文),用.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);
Run Code Online (Sandbox Code Playgroud)

这是生成通知的代码:

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);
Run Code Online (Sandbox Code Playgroud)

这是我得到的警告: 在此输入图像描述

jka*_*001 38

我想我已经学会了一些事情,这些都是答案:

  1. 我使用的是模拟器设备,其图像不包含Play商店.
  2. 图片上的Google Play服务版本不是最新版本,所以我应该收到通知告诉我需要升级.由于该通知未应用于频道,因此未显示.
  3. 如果我将Android Studio中的logcat设置为"No Filters"而不是"Show only selected application",那么我发现日志指出有问题的通知是Play Services"需要更新"通知.

所以,我更改为包含Play商店的图片,并且它正确地显示了通知(也许该通知的频道是由Play商店设置的?),让我更新到最新的Google Play服务,我的避风港从那以后就看到了那个警告.

所以,长话短说(太晚了) - 使用Android O,如果你在模拟器上使用Google Play服务和测试,请选择包含Play商店的图片,或忽略吐司(祝你好运!).


Gul*_*hat 7

首先创建通知渠道:

 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);
Run Code Online (Sandbox Code Playgroud)

然后在构造函数中使用通道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);
Run Code Online (Sandbox Code Playgroud)


Rob*_*net 6

我有同样的问题,并通过使用构造函数解决它

new Notification.Builder(Context context, String channelId),而不是在API级别> = 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);}
Run Code Online (Sandbox Code Playgroud)

  • 由于这个已经删除的构造函数,我有大约1k的崩溃...谢谢! (2认同)

Mat*_*tti 5

你必须先创建一个频道。

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());
}
Run Code Online (Sandbox Code Playgroud)

最后调用这个方法:

createNotificationChannel();
notifyThis("My notification", "Hello World!");
Run Code Online (Sandbox Code Playgroud)