有关startForeground的错误通知

时间:2018-12-17 12:28:04

标签: java android

我创建了一项服务。我启动了这个服务前台,但是当我在我的应用程序中吃午餐时,一个应用程序崩溃了。我添加了有关此内容的startServiceOreoCondition,但对我没有帮助

@Override
public void onCreate() {

    mdb = ManagerDB.getInstance(getApplicationContext(), "name");
    context = getApplicationContext();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_SERVICE, "*****", NotificationManager.IMPORTANCE_DEFAULT));
        nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID_INFO, "********", NotificationManager.IMPORTANCE_DEFAULT));
    }

    startServiceOreoCondition();
    sp = getSharedPreferences("pfref", Activity.MODE_PRIVATE);
    editor = sp.edit();
    executeSomething();
    timer = new Timer();
    }




     private void startServiceOreoCondition() {
            if (Build.VERSION.SDK_INT >= 26) {

                String NOTIFICATION_CHANNEL_ID = "pl.***.***";
                String channelName = "Communication Service";
                NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
                chan.setLightColor(Color.BLUE);
                chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                assert manager != null;
                manager.createNotificationChannel(chan);

                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
                Notification notification = notificationBuilder.setOngoing(true)
                        .setContentTitle(context.getString(R.string.app_name))
                        .setPriority(NotificationManager.IMPORTANCE_MIN)
                        .setCategory(Notification.CATEGORY_SERVICE)
                        .build();
                startForeground(4, notification);
            }else{
                startForeground(3, new Notification());
            }
        }

但是当我在Andorid 8.1上使用该应用程序时,我看到此错误:

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=1 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 category=service vis=PRIVATE)

我还添加了此权限:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

2 个答案:

答案 0 :(得分:1)

NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setDescription(<Add channel Description>);

您刚刚创建了Notification Channel对象,但是您需要使用channel对象创建频道。

    nm.createNotificationChannel(new NotificationChannel(NOTIFICATION_CHANNEL_ID, "********", NotificationManager.IMPORTANCE_DEFAULT));

在创建Notification Builder时,传递创建的通道ID。

    NotificationCompat.Builder notificationBuilder = new 
    NotificationCompat.Builder(context,NOTIFICATION_CHANNEL_ID);
            Notification notification = notificationBuilder.setOngoing(true)
                    .setContentTitle(context.getString(R.string.app_name))
                    .setPriority(NotificationManager.IMPORTANCE_MIN)
                    .setCategory(Notification.CATEGORY_SERVICE)
                    .build();

答案 1 :(得分:0)

您必须调用“ notification.setChannelId(NOTIFICATION_CHANNEL_ID)”才能为该特定通知分配频道