Android 9 Api 28 通知未显示
Posted
技术标签:
【中文标题】Android 9 Api 28 通知未显示【英文标题】:Android 9 Api 28 Notification not showing 【发布时间】:2019-05-27 09:45:30 【问题描述】:我正在尝试显示通知,但它不适用于 Oreo 和 Pie 版本。
它在 kitkat 版本中工作。
我尝试了所有可能的条件,我不知道我还缺少什么
这是我的代码:
String idChannel = "my_channel_01";
Intent mainIntent;
mainIntent = new Intent(ChecklistOptionActivity.this, CashCountOptionActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel mChannel = null;
// The id of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle(context.getString(R.string.app_name))
.setSmallIcon(R.drawable.bc_icon)
.setContentIntent(pendingIntent)
.setContentText("Test");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
mChannel = new NotificationChannel(idChannel, context.getString(R.string.app_name), importance);
// Configure the notification channel.
mChannel.setDescription("Test");
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setVibrationPattern(new long[]100, 200, 300, 400, 500, 400, 300, 200, 400);
mNotificationManager.createNotificationChannel(mChannel);
else
builder.setContentTitle(context.getString(R.string.app_name))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(ContextCompat.getColor(context, R.color.colorBlue))
.setVibrate(new long[]100, 250)
.setLights(Color.YELLOW, 500, 5000)
.setAutoCancel(true);
mNotificationManager.notify(1, builder.build());
【问题讨论】:
这能回答你的问题吗? Notification not showing in Oreo 【参考方案1】:String channel_id = createNotificationChannel(context);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channel_id);
下面的方法是生成一个新的channel_id。
public static String createNotificationChannel(Context context)
// NotificationChannels are required for Notifications on O (API 26) and above.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
// The id of the channel.
String channelId = "Channel_id";
// The user-visible name of the channel.
CharSequence channelName = "Application_name";
// The user-visible description of the channel.
String channelDescription = "Application_name Alert";
int channelImportance = NotificationManager.IMPORTANCE_DEFAULT;
boolean channelEnableVibrate = true;
// int channelLockscreenVisibility = Notification.;
// Initializes NotificationChannel.
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, channelImportance);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableVibration(channelEnableVibrate);
// notificationChannel.setLockscreenVisibility(channelLockscreenVisibility);
// Adds NotificationChannel to system. Attempting to create an existing notification
// channel with its original values performs no operation, so it's safe to perform the
// below sequence.
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);
return channelId;
else
// Returns null for pre-O (26) devices.
return null;
在这里,您将在包含 26+ SDK 版本的设备中使用 channel_id
收到推送通知。
因为 NotificationCompat.Builder(context)
方法已被弃用,因此您必须使用更新后的方法 NotificationCompat.Builder(context, channel_id)
,它有两个参数,一个用于上下文,第二个用于 channel_id。
在SDK版本为26+的设备中,可以每次创建channel_id。
【讨论】:
是的,此解决方案适用于我的项目和任何版本的设备。 奇怪的是我的新 NotificationCompat.Builder(context, channel_id);只允许上下文,当我添加 channel_id 时它会显示错误。 您的导入是 android.support.v4.app.NotificationCompat 吗?你使用 BroadcastReceiver 吗? 并使用com.android.support:support-compat:28.0.0
以上是关于Android 9 Api 28 通知未显示的主要内容,如果未能解决你的问题,请参考以下文章
Socket.io 无法在 Android 9(API 级别 28)上运行
Facebook Unity SDK AppRequest 未显示通知 [Android]
Xamarin - Oreo (API 26) Firebase Cloud Messaging 背景通知未显示在通知抽屉中