如何显示这样的通知或使用IMPORTANCE_UNSPECIFIED?

时间:2017-12-09 09:10:37

标签: java android android-notifications android-8.0-oreo

如何显示质量时间等通知。我使用下面的代码来显示通知,但它给了我IllegalArgumentException:无效的重要性级别

enter image description here

mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
CharSequence name = getString(R.string.app_name);
// Create the channel for the notification
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name,
        NotificationManager.IMPORTANCE_UNSPECIFIED);

// Set the Notification Channel for the Notification Manager.
mNotificationManager.createNotificationChannel(mChannel);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentTitle(getString(R.string.app_name))
        .setAutoCancel(true);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(
        this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

builder.setContentIntent(contentIntent);

Notification notification = builder.build();
startForeground(1, notification);

收到此错误:

java.lang.IllegalArgumentException: Invalid importance level 
at android.os.Parcel.readException(Parcel.java:1950)
at android.app.INotificationManager$Stub$Proxy.createNotificati‌onChannels(INotifica‌tionManager.java:155‌6)
at android.app.NotificationManager.createNotificationChannels(N‌otificationManager.j‌ava:453) 
at android.app.NotificationManager.createNotificationChannel(No‌tificationManager.ja‌va:441) 
at com.app.MyService.onCreate(ScreenService.java:167) –
Add Comment Collapse

1 个答案:

答案 0 :(得分:0)

您应该使用NotificationManager.IMPORTANCE_MIN

  

IMPORTANCE_MIN

     

在API级别24中添加   int IMPORTANCE_MIN   只显示在阴影下方的阴影下。

https://developer.android.com/reference/android/app/NotificationManager.html#IMPORTANCE_MIN