我查看了android文档,并在StackOverflow中看到了所有答案,但是,我似乎无法理解为什么我尝试显示的通知没有显示。每当我单击按钮时,应用程序就会崩溃,而不是显示通知,而有人可以告诉我为什么会发生这种情况吗?
notify_me.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("miscellaneous", "Hello World", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Hello Brothers and Sisters");
NotificationManager noti = getSystemService(NotificationManager.class);
noti.createNotificationChannel(channel);
}
}
}
);
这是堆栈跟踪
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test, PID: 27996
java.lang.IllegalArgumentException: Reserved id
at android.os.Parcel.createException(Parcel.java:1970)
at android.os.Parcel.readException(Parcel.java:1934)
at android.os.Parcel.readException(Parcel.java:1884)
at android.app.INotificationManager$Stub$Proxy.createNotificationChannels(INotificationManager.java:1888)
at android.app.NotificationManager.createNotificationChannels(NotificationManager.java:577)
at android.app.NotificationManager.createNotificationChannel(NotificationManager.java:565)
at com.example.test.Main4Activity$1.onClick(Main4Activity.java:44)
at android.view.View.performClick(View.java:7352)
at android.widget.TextView.performClick(TextView.java:14177)
at android.view.View.performClickInternal(View.java:7318)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27800)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.notification.RankingHelper.createNotificationChannel(RankingHelper.java:641)
at com.android.server.notification.NotificationManagerService$12.createNotificationChannelsImpl(NotificationManagerService.java:2585)
at com.android.server.notification.NotificationManagerService$12.createNotificationChannels(NotificationManagerService.java:2600)
at android.app.INotificationManager$Stub.onTransact(INotificationManager.java:292)
at android.os.Binder.execTransact(Binder.java:739)
答案 0 :(得分:2)
根据错误消息,您不能使用"miscellaneous"
作为通知通道的ID-该名称专门为不针对API 26或更高版本的应用程序保留,用于发布所有不具有API的通知连接到他们的频道。
您可以为频道使用其他任何ID字符串。