首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自创建NotificationChannel的IllegalArgumentException

来自创建NotificationChannel的IllegalArgumentException
EN

Stack Overflow用户
提问于 2021-12-03 11:58:27
回答 1查看 34关注 0票数 0

偶尔,在创建通知通道时,我们会在某些设备上收到带有空本地化消息的IllegalArgumentException。我找到了this答案,但是频道名称是100%确定的不是"",所以不是本例中的问题。它对几乎所有用户都有效,但有时会为某个用户抛出此异常。我们似乎也不能将它与设备类型或特定的Android版本联系起来。

创建通道的代码:

代码语言:javascript
复制
public void createNotificationChannel(String label, String description, int importance) {
    // Create the NotificationChannel only on API 26+
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(label, label, importance);
        channel.setDescription(description);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
}

异常的堆栈跟踪:

代码语言:javascript
复制
android.os.Parcel.createException(Parcel.java:2078)
android.os.Parcel.readException(Parcel.java:2042)
android.os.Parcel.readException(Parcel.java:1990)
android.app.INotificationManager$Stub$Proxy.createNotificationChannels(INotificationManager.java:3205)
android.app.NotificationManager.createNotificationChannels(NotificationManager.java:713)
android.app.NotificationManager.createNotificationChannel(NotificationManager.java:701)

有谁有什么想法吗?我很乐意解决这个问题!

EN

回答 1

Stack Overflow用户

发布于 2021-12-03 13:26:32

代码语言:javascript
复制
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);

        // Configure the notification channel
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.setVibrationPattern(new long[]{500, 500, 500, 500});
        notificationChannel.enableVibration(true);
        notificationManager.createNotificationChannel(notificationChannel);
    }

试试这段频道代码。它是工作代码。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70213945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档