Android 为什么通知在低于 Oreo 的版本上不起作用?
Android Why Notification is not working on below Oreo version?
I am refering this notficastion from this url of android documentation。尝试构建一个简单的通知,但似乎这段代码适用于奥利奥及以上。但是我应该如何处理以下版本的通知?
下面是我 运行 单击按钮的代码:
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 版本上工作正常,但我如何为以下版本添加?
如果我在下面的 oreo 版本中 运行 上面提到的代码,下面是我得到的异常:
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)
将通知小图标放入可绘制文件夹中。
To provide support for all Android versions, developers should:
Place status bar icons for Android 3.0 and later in the drawable...
使用这种类型的通知....
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 块的编码是为你准备的,它一定会对你有所帮助
I am refering this notficastion from this url of android documentation。尝试构建一个简单的通知,但似乎这段代码适用于奥利奥及以上。但是我应该如何处理以下版本的通知?
下面是我 运行 单击按钮的代码:
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 版本上工作正常,但我如何为以下版本添加?
如果我在下面的 oreo 版本中 运行 上面提到的代码,下面是我得到的异常:
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)
将通知小图标放入可绘制文件夹中。
To provide support for all Android versions, developers should: Place status bar icons for Android 3.0 and later in the drawable...
使用这种类型的通知....
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 块的编码是为你准备的,它一定会对你有所帮助