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

时间:2017-06-11 23:29:30

标签: android notifications 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);

以下是生成通知的代码:

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);

这是我得到的警告: enter image description here

8 个答案:

答案 0 :(得分:32)

我想我已经学到了一些可以得到答案的东西:

  1. 我使用的是模拟器设备,图片中没有Play商店。
  2. 图片上的Google Play服务版本不是最新版本,所以我应该收到通知告诉我需要升级。由于该通知未应用于频道,因此未显示。
  3. 如果我将Android Studio中的logcat设置为“No Filters”而不是“仅显示选定的应用程序”,那么我发现日志指出有问题的通知是Play服务“需要更新”通知。
  4. 所以,我更改为包含Play商店的图片,它显示通知正确(也许该通知的频道是由Play商店设置的?),让我更新到最新的Google Play服务,从那以后我就没有看到那个警告。

    所以,长话短说(太迟了) - 使用Android O,如果您使用的是Google Play服务&在模拟器上进行测试,选择包含Play商店的图像,或忽略吐司(祝你好运!)。

答案 1 :(得分: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);

然后在构造函数中使用通道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);

答案 2 :(得分:6)

我遇到了同样的问题,并使用构造函数

解决了这个问题

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);}

答案 3 :(得分: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!");

答案 4 :(得分:2)

使用以下代码创建通知:

    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();

答案 5 :(得分:1)

您必须先创建一个NotificationChannel

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

这是显示API 26 +

通知的唯一方法

答案 6 :(得分:0)

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

答案 7 :(得分:-4)

我还想补充一点,如果您使用的是构建工具v26 +,则会收到此错误:

应用/ build.grade

compileSdkVersion 26
buildToolsVersion "26.0.2"

defaultConfig {
    targetSdkVersion 26

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