【问题标题】:IllegalArgumentException from creating a NotificationChannel来自创建 NotificationChannel 的 IllegalArgumentException
【发布时间】:2022-01-09 19:12:07
【问题描述】:

在创建通知通道时,我们偶尔会在某些设备上收到带有空本地化消息的 IllegalArgumentException。我找到了this 的答案,但是频道名称 100% 肯定不是“”,所以在这种情况下不是问题。它适用于几乎所有用户,但有时会为用户抛出此异常。我们似乎也无法将其与设备类型或特定 Android 版本相关联。

创建频道的代码:

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

异常的堆栈跟踪:

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)

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

【问题讨论】:

    标签: android android-notifications notification-channel


    【解决方案1】:
    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);
        }
    

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

    【讨论】:

    • 嗨,Ammad,感谢您的思考,但这正是我的代码中发生的事情(除了灯光、颜色、振动模式和振动方法)。在极少数情况下会在createNotificationChannel 上引发错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2013-01-02
    • 2015-11-02
    • 1970-01-01
    • 2014-08-29
    相关资源
    最近更新 更多