【问题标题】:Why does the notification not show up in API 28?为什么通知没有出现在 API 28 中?
【发布时间】:2019-11-20 09:07:05
【问题描述】:

我查看了 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)

【问题讨论】:

  • “应用程序关闭”到底是什么意思?它崩溃了吗?您是否在 logcat 中看到异常?
  • 我在屏幕底部收到一条错误消息,显示“testApp 一直在停止”。
  • 这意味着它崩溃了。发布堆栈跟踪。
  • 我已经发布了堆栈跟踪

标签: java android android-studio android-intent android-notifications


【解决方案1】:

根据错误消息,您不能使用 "miscellaneous" 作为通知通道的 ID - 该名称专为不针对 API 26 或更高版本的应用保留,用于发布所有没有通道的通知附在他们身上。

您可以为您的频道使用任何其他 ID 字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2011-07-27
    • 2022-01-20
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多