用于前台服务的Android 8通知通道

时间:2017-12-01 11:03:53

标签: android notifications android-8.0-oreo

我想知道我的通知创建源的以下修改是否符合Android 8。 之前:

Notification.Builder builder = new Notification.Builder(this);
// ... (some code omitted)
myNotification = builder.build();
// ... (some code omitted)
startForeground(MY_NOTIFICATION_ID, myNotification);

后:

Notification.Builder builder;
if ( Build.VERSION.SDK_INT < 26 ) {
    builder = new Notification.Builder(this);
} else {
    final String ChannelId = "mypackage.NotificationChannelID";
    final CharSequence ChannelName = "My Notification";
    NotificationChannel channel = new NotificationChannel(ChannelId, ChannelName, NotificationManager.IMPORTANCE_LOW);
    ((NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE)).
        createNotificationChannel(channel);
    builder = new Notification.Builder(this, ChannelId);
}
// ... (some code omitted)
myNotification = builder.build();
// ... (some code omitted)
startForeground(MY_NOTIFICATION_ID, myNotification);

特别是,至少还有两个问题:

(1)我想知道是否以及在哪里添加void deleteNotificationChannel(String channelId)以不产生任何内存泄漏?

(2)我应该添加builder.setCategory(Notification.CATEGORY_SERVICE),如果是,为什么?

0 个答案:

没有答案