Why does the notification not show up in API 28?

二次信任 提交于 2020-02-14 21:36:49

问题


I have looked at android documentation and I have seen all the answers in StackOverflow, however, I cannot seem to understand why the notification I am trying to show not show up. Whenever I click the button, instead of notification showing up, the app crashes, can someone please give me insight as to why this is occurring?

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);
                    }
                }
            }
    );

Here is the stack trace

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)

回答1:


As per the error message, you cannot use "miscellaneous" as the ID of your notification channel - that name is reserved specifically for apps that don't target API 26 or higher for posting all notifications that don't have a channel attached to them.

You can use any other id string for your channel.



来源:https://stackoverflow.com/questions/56975768/why-does-the-notification-not-show-up-in-api-28

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!