创建通知通道时抛出`illegalargumentexception`

我正在尝试向我的应用添加通知警报,但我不断收到似乎无法弄清的错误。它是illegalargumentexception,被扔在notificationmanagerager.createNotificationChannel(channel);上,我不知道它要我修复什么。我在网上看了一堆教程,但似乎无法弄清楚是什么原因导致了这个问题。请帮我。

我正在使用API​​ 26。

package com.example.mytermtracker.notifications;


import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.Notificationmanager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.example.mytermtracker.R;
import com.example.mytermtracker.application.Date;

public class CustomReceiver extends BroadcastReceiver {
    private static final String TAG = "app: CustomReceiver";

    private static int id = 0;
    private static final String CHANNEL_ID = "com.example.mytermtracker";

    public static final String TEST_CHANEL = "";

    @Override
    public void onReceive(Context context,Intent intent) {
        Log.d(TAG,"onReceive: " + Date.now().toSQL());
        String channelName = intent.getStringExtra("channel_name");
        if (channelName == null) channelName = "";

        String title = intent.getStringExtra("title");
        if (title == null) title = "";

        String message = intent.getStringExtra("message");
        if (message == null) message = "";

        String description = intent.getStringExtra("description");
        if (description == null) description = "";

        int lockScreenVisibility = intent.getIntExtra("lock_screen_visibility",Notification.VISIBILITY_PUBLIC);

        Notification notification = new NotificationCompat.Builder(context,channelName)
                .setSmallIcon(R.drawable.ic_notification_icon)
                .setContentTitle(title)
                .setContentText(message)
                .build();

        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,channelName,Notificationmanager.IMPORTANCE_DEFAULT);
        channel.setDescription(description);
        channel.setLockscreenVisibility(lockScreenVisibility);

        Notificationmanager notificationmanagerager = (Notificationmanager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationmanagerager != null) {
            notificationmanagerager.createNotificationChannel(channel); //<====== Exception is being thrown on this line.
            notificationmanagerager.notify(id++,notification);
        }
    }

    public enum Channel {
        C001 ("c001"),C002 ("c002")
        ;

        private String name;

        Channel(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    public static Builder create(Context context,Channel channel,Date triggertime) {
        return new Builder(context,channel,triggertime);
    }

    public static class Builder {
        private Context context;
        private Channel channel;
        private Date triggertime;
        private String title;
        private String message;
        private String description;
        private int lockScreenVisibility;

        private Builder(Context context,Date triggertime) {
            this.context = context;
            this.channel = channel;
            this.triggertime = triggertime;
            this.title = "";
            this.message = "";
            this.description = "";
            this.lockScreenVisibility = Notification.VISIBILITY_PUBLIC;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setDescription(String description) {
            this.description = description;
            return this;
        }

        public Builder setLockScreenVisibility(int lockScreenVisibility) {
            this.lockScreenVisibility = lockScreenVisibility;
            return this;
        }

        public void done() {
            Intent intent = new Intent(context,CustomReceiver.class);
            intent.putExtra("channel",channel.name);
            intent.putExtra("title",title);
            intent.putExtra("message",message);
            intent.putExtra("description",description);
            intent.putExtra("lock_screen_visibility",lockScreenVisibility);

            Log.d(TAG,"done: " + Date.now().toSQL());

            PendingIntent sender = PendingIntent
                    .getBroadcast(context,id++,intent,PendingIntent.flaG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

            Log.d(TAG,"done: trigger: " + triggertime.toSQL());

            alarmManager.set(AlarmManager.RTC_WAKEUP,triggertime.getTimeInmillis(),sender);
        }
    }
}
dongyafei123 回答:创建通知通道时抛出`illegalargumentexception`

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3087968.html

大家都在问