Android 8.1.0 前台服务通知问题

标签 android android-service android-notifications foreground-service

[我遇到了已经在 stackoverflow 上报告过的相同问题][1]当在 8.1.0 中启动 forground 服务时,我收到了应用程序崩溃的通知。错误日志如下。它在 android 8.0 和 android 9 中工作正常。 任何人都面临这个问题。

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    String input = intent.getStringExtra("inputExtra");
    createNotificationChannel();
    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0, notificationIntent, 0);
    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Foreground Service")
            .setContentText(input)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentIntent(pendingIntent)
            .build();
    startForeground(1, notification);
    //do heavy work on a background thread
    //stopSelf();
    return START_NOT_STICKY;
}

错误

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 defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                at android.os.Handler.dispatchMessage(Handler.java:106)
                at android.os.Looper.loop(Looper.java:164)
                at android.app.ActivityThread.main(ActivityThread.java:6626)
                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:811)


  [1]: https://stackoverflow.com/questions/47531742/startforeground-fail-after-upgrade-to-android-8-1

最佳答案

private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT

//Try adding this line inside If condition.

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
            );
        }
    }

关于Android 8.1.0 前台服务通知问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59396037/

相关文章:

Android FCM 与 Azure 通知中心错误 FirebaseInstanceId

java - 从 PHP/MySQL 到 Java/JSON

android - 如何在从服务类更新的 Activity 的文本字段中显示值?

android - 在 android 中发送短信作为服务

android - 如何在 android 中以编程方式发送 whatsapp 消息?

Android,当应用程序打开时显示 alertDialog 而不是通知

android - 如何以编程方式关闭/取消android中的状态栏通知

android - 如何在 Delphi 中使用自定义 debug.keystore?

android - 我可以在一个地方看到所有的字符串值(资源)吗?

android - 有没有办法将正在进行的 Android 应用程序发送给某人进行测试?