42

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

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

4

8 回答 8

42

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

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

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

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

于 2017-06-13T02:39:28.460 回答
7

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

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);}
于 2017-08-19T17:04:47.003 回答
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);
于 2017-10-10T04:23:59.660 回答
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());
}

最后你调用这个方法:

createNotificationChannel();
notifyThis("My notification", "Hello World!");
于 2018-09-22T19:55:05.527 回答
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();
于 2017-07-20T05:58:19.670 回答
1

您必须先创建一个 NotificationChannel

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

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

于 2018-01-19T09:19:13.177 回答
0

我面临着同样的问题。它通过创建 NotificationChannel 并使用通知管理器添加新创建的通道来解决。

于 2018-01-23T22:57:28.613 回答
-4

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

应用程序/build.grade

compileSdkVersion 26
buildToolsVersion "26.0.2"

defaultConfig {
    targetSdkVersion 26

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

于 2018-04-20T08:29:56.710 回答