startForeground的频道无效

时间:2018-11-27 11:09:49

标签: android android-notifications foreground-service

我在生产中遇到了崩溃,无法重现,也不知道为什么会发生。

android.app.RemoteServiceException: (Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x4a color=0x00000000 vis=PRIVATE))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

在我看来,该频道并未创建,但是每次创建通知之前我都会创建一个频道。

我什至不确定它是否在代码的这一部分中发生,但这是我启动前台服务的唯一位置,因此我认为自己处在正确的轨道上

这是代码

String channelId = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_ID;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String channelName = NotificationChannelConstants.MAIN_NOTIFICATION_CHANNEL_NAME;
    NotificationChannel chan = new NotificationChannel(channelId,
                channelName, NotificationManager.IMPORTANCE_LOW);
    chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager service = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    service.createNotificationChannel(chan);
}

notificationBuilder = new NotificationCompat.Builder(this, channelId)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.sb_unknown)
            .setContentTitle(getString(R.string.app_name))
            .setTicker(getString(R.string.app_name)).setContentIntent(broadcastIntent)
            .setContentText(mNotificationText)
            .setAutoCancel(false)
            .setOngoing(true)
            .setOnlyAlertOnce(true);

startForeground(R.id.notification, notificationBuilder.build());

1 个答案:

答案 0 :(得分:2)

您可以将其用于Oreo及更高版本

如果您使用服务类,则可以输入此代码

@Override
public void onCreate(){
    super.onCreate();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        startMyOwnForeground();
    else
        startForeground(1, new Notification());
}

private void startMyOwnForeground(){
    String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
    String channelName = "My Background 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(this, NOTIFICATION_CHANNEL_ID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.drawable.icon_1)
            .setContentTitle("App is running in background")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();
    startForeground(2, notification);
} 

当然不要忘记您的AndroidManifest.xml的ForeGroundService权限

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