gpt4 book ai didi

java - Android 9 Api 28 通知未显示

转载 作者:行者123 更新时间:2023-12-02 09:51:00 24 4
gpt4 key购买 nike

我正在尝试显示通知,但它不适用于 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());

最佳答案

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。
  • 在具有26+ SDK版本的设备中,您可以每次创建channel_id。

关于java - Android 9 Api 28 通知未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56323523/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com