将我的项目升级到 Android O
buildToolsVersion "26.0.1"
Android Studio中的Lint显示以下通知构建器方法的已弃用警告:
new NotificationCompat.Builder(context)
问题在于: Android开发者更新了描述 NotificationChannel 的文档,以支持Android O中的通知,并向我们提供了一个代码段,但同样的弃用警告:< / p>
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
我的问题:是否有其他解决方案可用于构建通知,并且仍然支持Android O?
我找到的解决方案是将通道ID作为Notification.Builder构造函数中的参数传递。但是这个解决方案并不完全可以重复使用。
new Notification.Builder(MainActivity.this, "channel_id")
答案 0 :(得分:123)
文档中提到构建器方法NotificationCompat.Builder(Context context)
已被弃用。我们必须使用具有channelId
参数的构造函数:
NotificationCompat.Builder(Context context, String channelId)
https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
在API级别26.0.0-beta1中不推荐使用此构造函数。使用 改为NotificationCompat.Builder(Context,String)。全部发布 通知必须指定NotificationChannel Id。
https://developer.android.com/reference/android/app/Notification.Builder.html
这个构造函数在API级别26中已弃用。使用 而是Notification.Builder(Context,String)。全部发布 通知必须指定NotificationChannel Id。
如果要重用构建器设置器,可以使用channelId创建构建器,并将该构建器传递给帮助器方法并在该方法中设置首选设置。
答案 1 :(得分:98)
以下是所有Android版本的工作代码,从 API LEVEL 26 + 开始,具有向后兼容性。
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getContext(), "M_CH_ID");
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Hearty365")
.setPriority(Notification.PRIORITY_MAX) // this is deprecated in API 26 but you can still use for below 26. check below update for 26 API
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
.setContentInfo("Info");
NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
更新API 26以设置最高优先级
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_MAX);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("Hearty365")
// .setPriority(Notification.PRIORITY_MAX)
.setContentTitle("Default notification")
.setContentText("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
.setContentInfo("Info");
notificationManager.notify(/*notification id*/1, notificationBuilder.build());
答案 2 :(得分:27)
调用2-arg构造函数:为了与Android O兼容,请调用support-v4 NotificationCompat.Builder(Context context, String channelId)
。在Android N或更早版本上运行时,channelId
将被忽略。在Android O上运行时,还要使用相同的NotificationChannel
创建channelId
。
过时示例代码:多个JavaDoc网页上的示例代码(例如Notification.Builder调用new Notification.Builder(mContext)
已过期。
弃用的构造函数: Notification.Builder(Context context)
和 v4 NotificationCompat.Builder(Context context)
已弃用,而不是Notification[Compat].Builder(Context context, String channelId)
。 (请参阅Notification.Builder(android.content.Context)和v4 NotificationCompat.Builder(Context context)。)
弃用的课程:不推荐使用整个班级 v7 NotificationCompat.Builder
。 (请参阅v7 NotificationCompat.Builder。)以前,需要v7 NotificationCompat.Builder
来支持NotificationCompat.MediaStyle
。在Android O中,media-compat library的NotificationCompat.MediaStyle
包中有v4 android.support.v4.media
。如果您需要MediaStyle
,请使用那个。
API 14 +:在26.0.0及更高版本的支持库中,support-v4和support-v7软件包都支持最低API级别14. v#名称是历史名称。< / p>
答案 3 :(得分:19)
而不是像Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
那样多的答案建议,而是采用一种稍微简单的方式 -
将以下行添加到 AndroidManifest.xml 文件的application
部分,如Set Up a Firebase Cloud Messaging Client App on Android doc中所述:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id" />
然后在 values / strings.xml 文件中添加一个包含频道名称的行:
<string name="default_notification_channel_id">default</string>
之后,您将能够使用带有2个参数的NotificationCompat.Builder构造函数的新版本(因为在Android Oreo中不推荐使用带有1个参数的旧构造函数):
private void sendNotification(String title, String body) {
Intent i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pi = PendingIntent.getActivity(this,
0 /* Request code */,
i,
PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
getString(R.string.default_notification_channel_id))
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pi);
NotificationManager manager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
答案 4 :(得分:13)
以下是示例代码,该代码适用于Android Oreo且低于Oreo。
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel("ID", "Name", importance);
notificationManager.createNotificationChannel(notificationChannel);
builder = new NotificationCompat.Builder(getApplicationContext(), notificationChannel.getId());
} else {
builder = new NotificationCompat.Builder(getApplicationContext());
}
builder = builder
.setSmallIcon(R.drawable.ic_notification_icon)
.setColor(ContextCompat.getColor(context, R.color.color))
.setContentTitle(context.getString(R.string.getTitel))
.setTicker(context.getString(R.string.text))
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);
notificationManager.notify(requestCode, builder.build());
答案 5 :(得分:6)
public void showNotification (String from, String notification, Intent intent) {
PendingIntent pendingIntent = PendingIntent.getActivity(
context,
Notification_ID,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
String NOTIFICATION_CHANNEL_ID = "my_channel_id_01";
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
// Configure the notification channel.
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
Notification mNotification = builder
.setContentTitle(from)
.setContentText(notification)
// .setTicker("Hearty365")
// .setContentInfo("Info")
// .setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
// .setDefaults(Notification.DEFAULT_ALL)
// .setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
.build();
notificationManager.notify(/*notification id*/Notification_ID, mNotification);
}
答案 6 :(得分:2)
Notification notification = new Notification.Builder(MainActivity.this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
.setChannelId(CHANNEL_ID)
.build();
正确的代码是:
Notification.Builder notification=new Notification.Builder(this)
具有依赖关系26.0.1和新更新的依赖关系,例如28.0.0。
有些用户以此形式使用此代码:
Notification notification=new NotificationCompat.Builder(this)//this is also wrong code.
因此逻辑是您将声明或初始化的方法,然后右侧的相同方法将用于分配。如果在Leftside中=你将使用某种方法,则在=的右侧使用相同的方法进行新的分配。
试试这段代码......一定会有效
答案 7 :(得分:1)
在API级别26.1.0中不推荐使用此构造函数。 请改用NotificationCompat.Builder(Context,String)。所有发布的通知都必须指定NotificationChannel ID。
答案 8 :(得分:0)
...
public static final String NOTIFICATION_CHANNEL_ID = MyLocationService.class.getSimpleName();
...
...
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_ID+"_name",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager notifManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notifManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.notification_text))
.setOngoing(true)
.setContentIntent(broadcastIntent)
.setSmallIcon(R.drawable.ic_tracker)
.setPriority(PRIORITY_HIGH)
.setCategory(Notification.CATEGORY_SERVICE);
startForeground(1, builder.build());
...