[英]Android Why Notification is not working on below Oreo version?
我是從android文檔的這個URL引用此通知 。 嘗試構建一個簡單的通知,但似乎此代碼適用於oreo及以上版本。 但是我應該如何處理以下版本的通知?
下面是我在單擊按鈕時運行的代碼:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, PRIMARY_CHANNEL)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("Much longer text that cannot fit one line...")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Much longer text that cannot fit one line..."))
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.feroz_channel_name);
String description = getString(R.string.feroz_channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(PRIMARY_CHANNEL, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager1 = getSystemService(NotificationManager.class);
notificationManager1.createNotificationChannel(channel);
}
// notificationId is a unique int for each notification that you must define
notificationManager.notify(123, mBuilder.build());
它可以在android oreo和pie版本上正常工作,但是如何為以下版本提供呢?
以下是我在以下奧利奧版本中運行上述代碼時遇到的異常:
Process: sample.androido.com.myapplication, PID: 14596
android.app.RemoteServiceException: Bad notification posted from package sample.androido.com.myapplication: Couldn't create icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=sample.androido.com.myapplication id=0x7f0f0000) visible user=0 )
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
使用這種類型的通知。
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
Notification builder = new NotificationCompat.Builder(ctx,
notification.CHANNEL_ID_ONE)
.setContentTitle(title)
.setContentText(Artist)
.setLargeIcon(bitmap1)
.setSmallIcon(R.drawable.back)
.addAction(R.drawable.play,"back",pi)
.addAction(R.drawable.play,"play",pendingIntent)
.addAction(R.drawable.play,"next",PI)
.build();
NotificationManager notificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(111, builder);
}else{
Notification notification = new NotificationCompat.Builder(ctx)
.setContentTitle(title)
.setContentText(Artist)
.setLargeIcon(bitmap1)
.setSmallIcon(R.drawable.back)
.addAction(R.drawable.play,"back",pi)
.addAction(R.drawable.play,"play",pendingIntent)
.addAction(R.drawable.play,"next",PI)
.build();
NotificationManager notificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(111, notification);
}
您知道如何在奧利奧(Oreo)中設置通知,因此else塊的編碼非常適合您,它一定會對您有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.