【问题标题】:Notifications are not shown on Android 8 and 9Android 8 和 9 上不显示通知
【发布时间】:2019-04-10 18:39:47
【问题描述】:

我正在开发一个包含通知的Android 应用程序。我试过新的Android 8通知码:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "Channel"+notificationId)
                .setSmallIcon(R.drawable.ic_notification_logo)
                .setContentTitle(notification.getTitle())
                .setContentText(notification.getBody())
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setChannelId("Channel"+notificationId);

NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String CHANNEL_ID = "Channel" + notificationId;// The id of the channel.
        CharSequence name = "MyApp";// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        notificationManager.createNotificationChannel(mChannel);
    }

    notificationManager.notify(notificationId, notification);

但仍然无法正常工作,并且在 8 和 9 上都没有显示任何通知。

【问题讨论】:

  • 它是否适用于比奥利奥更早的版本?另外,应用程序是否在设置中启用了通知?
  • 是的,它适用于 Android 7 和 6。允许通知是的。

标签: android android-notifications android-8.0-oreo notificationmanager


【解决方案1】:

我相信你需要更换

notificationManager.notify(notificationId, notification);

notificationManager.notify(notificationId, notificationBuilder.build());

让它工作。

【讨论】:

  • 是的,这确实是问题所在,还有另一个错误的方法调用。谢谢
猜你喜欢
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 2023-03-04
  • 1970-01-01
  • 2020-03-09
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
相关资源
最近更新 更多