如果你得到这个错误应该注意的2项,并命令他们:
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
builder = new NotificationCompat.Builder(this, id);
而且NotificationManager notifManager和NotificationChannel mChannel创建只有一次。和setChannelId(ID)所需的在API级别> = 26构造.Builder弃用(本)。
如果在API含量使用弃用> = 26构造.Builder(本)然后builder.setChannelId(ID)是必需的Android 8奥利奥API 26和更高版本。
有所需的通知设置器在Android 8奥利奥API 26和更高版本:
- builder.setContentTitle()//需要
- .setSmallIcon()//需要
- .setContentText() //需要
- .setChannelId(ID)//用于弃用API水平所需> = 26构造.Builder(本)
例如:然而
private NotificationManager notifManager;
public void createNotification(String aMessage) {
final int NOTIFY_ID = 1002;
// There are hardcoding only for show it's just strings
String name = "my_package_channel";
String id = "my_package_channel_1"; // The user-visible name of the channel.
String description = "my_package_first_channel"; // The user-visible description of the channel.
Intent intent;
PendingIntent pendingIntent;
NotificationCompat.Builder builder;
if (notifManager == null) {
notifManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = notifManager.getNotificationChannel(id);
if (mChannel == null) {
mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
notifManager.createNotificationChannel(mChannel);
}
builder = new NotificationCompat.Builder(this, id);
intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentTitle(aMessage) // required
.setSmallIcon(android.R.drawable.ic_popup_reminder) // required
.setContentText(this.getString(R.string.app_name)) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setTicker(aMessage)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
} else {
builder = new NotificationCompat.Builder(this);
intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentTitle(aMessage) // required
.setSmallIcon(android.R.drawable.ic_popup_reminder) // required
.setContentText(this.getString(R.string.app_name)) // required
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setTicker(aMessage)
.setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
.setPriority(Notification.PRIORITY_HIGH);
} // else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Notification notification = builder.build();
notifManager.notify(NOTIFY_ID, notification);
}
邮政代码请 –
的可能的复制[通知失败中的Android奥利奥显示(API 26)](HTTPS:/ /stackoverflow.com/questions/45395669/notifications-fail-to-display-in-android-oreo-api-26) –