Android 8.1 版,startForeground 的错误通知

Dnv*_*raj 3 java android push-notification firebase

我已经给了我几天的时间来解决这个问题。我的应用程序崩溃,其 android 版本为 8.1。它在 Android 8.0 版中运行良好。

我按照下面提到的解决方案链接并尝试了此链接中给出的许多解决方案,但我的应用程序没有打开。

升级到 Android 8.1 后 startForeground 失败

我在 FirebaseMessageService 类中检查了 Android 版本并为 Oreo 版本创建了频道,但每次都显示以下给定的错误

 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:1795)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:171)
        at android.app.ActivityThread.main(ActivityThread.java:6649)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
Run Code Online (Sandbox Code Playgroud)

下面是我的代码。请帮我解决这个问题

    public class MyFirebaseMessagingService extends FirebaseMessagingService
    {
        private NotificationManager notifManager;
        Context mContext;
        Random random = new Random();
        int m = random.nextInt(9999 - 1000) + 1000;

        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) 
        {
           sendNotification();
        }



   public void sendNotification()
   {
   Intent intent = new Intent(this, Home.class);
   intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   PendingIntent pendingIntent = PendingIntent.getActivity(this, m /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT);

   String channelId = getString(R.string.default_notification_channel_id);
   Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
   NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                   .setSmallIcon(R.drawable.app_icon)
                   .setContentTitle("Logipace")
                   .setStyle(new NotificationCompat.BigTextStyle()
                           .bigText(message))
                   .setContentText(message)
                   .setAutoCancel(true)
                   .setSound(defaultSoundUri)
                   .setContentIntent(pendingIntent);
   notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);

   NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
    {
        NotificationChannel channel = new NotificationChannel(channelId, "LOGIPACE", NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(m /* ID of notification */, notificationBuilder.build());

    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}
    }
Run Code Online (Sandbox Code Playgroud)

下面是我在 Activity 类的 onCreate() 方法中使用的代码。

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            // Create channel to show notifications.
            String channelId  = getString(R.string.default_notification_channel_id);
            String channelName = getString(R.string.default_notification_channel_name);
            NotificationManager notificationManager =
                    getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(new NotificationChannel(channelId,
                    channelName, NotificationManager.IMPORTANCE_LOW));
        }
Run Code Online (Sandbox Code Playgroud)

帮我解决这个问题。

小智 8

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

如果您使用服务类,则可以放置此代码

public class BlablaService extends Service 

    @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);
    } 
Run Code Online (Sandbox Code Playgroud)

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

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
Run Code Online (Sandbox Code Playgroud)