通知未在 Android 8 上显示
Posted
技术标签:
【中文标题】通知未在 Android 8 上显示【英文标题】:Notification not showing up on Android 8 【发布时间】:2018-12-23 17:03:59 【问题描述】:我已阅读有关此问题的大多数问题/答案,但我的通知仍未显示。
我的广播接收器被调用,但没有任何反应。没有错误消息也没有异常。这是我的代码:
public class AlarmReceiver extends BroadcastReceiver
private static int notificationId = 0;
@Override
public void onReceive(Context context, Intent intent)
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 101, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context, "mychannel")
.setSmallIcon(R.drawable.icon_awake)
.setContentTitle("MyAlarm")
.setContentText("Something")
.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
notificationManager.notify(notificationId++, mNotifyBuilder.build());
【问题讨论】:
【参考方案1】:我认为您忘记在 Oreo 8.0 上阅读 Google 文档。 如果您想在 8.0 上创建通知,您需要使用 NotificationChannel
像这样:
NotificationChannel channel = null;
String chanel = "MY_Musixbox";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
int importance = NotificationManager.IMPORTANCE_LOW;
channel = new NotificationChannel(chanel, "MusicBox", importance);
notificationManager.createNotificationChannel(channel);
然后添加您的通知
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context, "mychannel")
.setSmallIcon(R.drawable.icon_awake)
.setContentTitle("MyAlarm")
.setContentText("Something")
.setChannelId(chanel);
.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent);
notificationManager.notify(notificationId++, mNotifyBuilder.build());
【讨论】:
【参考方案2】:好的,我会自己回答,以防有人发现:看来您也需要自己手动创建通知通道:
if (Build.VERSION.SDK_INT >= 26)
NotificationChannel channel = new NotificationChannel("mychannel",
"Channel name",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Channel description");
notificationManager.createNotificationChannel(channel);
【讨论】:
【参考方案3】:NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 101, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
context, "mychannel")
.setSmallIcon(R.drawable.icon_awake)
.setContentTitle("MyAlarm")
.setContentText("Something")
.setAutoCancel(false)
.setWhen(System.currentTimeMillis())
.setChannelId("default_channel")
.setContentIntent(pendingIntent);
notificationManager.notify(notificationId++, mNotifyBuilder.build());
试试这个。您必须定义频道 ID。这在 android 8.0 中是强制性的
【讨论】:
以上是关于通知未在 Android 8 上显示的主要内容,如果未能解决你的问题,请参考以下文章
Onesignal通知显示在OneSignal仪表板上,但未在通知栏中显示(android)