在没有 AppCompat 的情况下显示 Android O 及更高版本的通知
Show notification for Android O and above without AppCompat
所以我有没有任何库的超轻应用程序,除了 Crashlytics。
我想在 Android 的所有版本上显示通知,但是没有构造函数或方法来为 Android O+ 设置频道 ID。
我的代码现在看起来像这样
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
//Some builder things
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("service_id",
context.getString(R.string.app_name),
NotificationManager.IMPORTANCE_DEFAULT);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
}
Notification notification = builder.getNotification();
NotificationManagerCompat.from(context).notify(id, notification);
是否有任何仅用于通知的 compat lite 库?
Notification.Builder
和 NotificationCompact.Builder
都可以做到这一点:
Notification.Builder(context, "channelId")
NotificationCompat.Builder(context, "channelId")
他们有 setChannelId
方法。
所以我有没有任何库的超轻应用程序,除了 Crashlytics。
我想在 Android 的所有版本上显示通知,但是没有构造函数或方法来为 Android O+ 设置频道 ID。
我的代码现在看起来像这样
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
//Some builder things
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("service_id",
context.getString(R.string.app_name),
NotificationManager.IMPORTANCE_DEFAULT);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
}
Notification notification = builder.getNotification();
NotificationManagerCompat.from(context).notify(id, notification);
是否有任何仅用于通知的 compat lite 库?
Notification.Builder
和 NotificationCompact.Builder
都可以做到这一点:
Notification.Builder(context, "channelId")
NotificationCompat.Builder(context, "channelId")
他们有 setChannelId
方法。