Push notification is not working in Oreo (8.1.0) after the app is closed

0

Android: 8.1.0 Device: Infinix X604B

Problem: Using "Notification Composer" in Firebase console to send notification to my signed release app. Things works when my app is in foreground / background. When I close app by swapping it out from open app list, notifications sent are not received.

I get following in my logcat:

2019-01-18 12:22:21.758 2015-11920/? I/ActivityManager: Killing 15197:com.tsp.fcm/u0a148 (adj 900): remove task

2019-01-18 12:22:21.831 2015-2108/? W/InputDispatcher: channel '92faa4f com.tsp.fcm/com.tsp.fcm.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9

2019-01-18 12:22:21.831 2015-2108/? E/InputDispatcher: channel '92faa4f com.tsp.fcm/com.tsp.fcm.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

2019-01-18 12:22:21.831 2015-2151/? I/WindowManager: WIN DEATH: Window{92faa4f u0 com.tsp.fcm/com.tsp.fcm.MainActivity} 2019-01-18 12:22:21.832 2015-2151/? W/InputDispatcher: Attempted to unregister already unregistered input channel '92faa4f com.tsp.fcm/com.tsp.fcm.MainActivity (server)'

2019-01-18 12:22:21.837 2015-3498/? V/ActivityManager: Dying proc: com.tsp.fcm,pid:15197 was not allowed to restart.

2019-01-18 12:22:37.405 2015-2030/? D/ProcessManager.AS: *** Skip {com.tsp.fcm} to receive broadcast.

2019-01-18 12:22:37.406 2015-2030/? D/BroadcastQueue: *** Not launch app com.tsp.fcm/10148 for broadcast Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x11000010 pkg=com.tsp.fcm (has extras) } from com.google.android.gms/10020.(AutoStart limited)

2019-01-18 12:22:37.413 2727-2727/? W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.tsp.fcm (has extras) }

android
push-notification
firebase-cloud-messaging
asked on Stack Overflow Jan 18, 2019 by Ashish Khurange • edited Jan 18, 2019 by Phantômaxx

1 Answer

0

Android 8.0 introduces notification channels that allow you to create a user-customizable channel for each type of notification you want to display.

We can separate the behaviour of notifications by creating the channels for each of them. If no channel is specified for a notification, this notification will not appear in the status bar.

Before Android Oreo when we wanted to create a notification we had to do something like this:

    /**Creates an explicit intent for an Activity in your app**/
    Intent resultIntent = new Intent(mContext , SomeOtherActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
            0 /* Request code */, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder = new NotificationCompat.Builder(mContext);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(false)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent);

    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());

Now with Android Oreo, to display one notification we will have to do something like this (Check added if):

        /**Creates an explicit intent for an Activity in your app**/
        Intent resultIntent = new Intent(mContext , SomeOtherActivity.class);
        resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
                0 /* Request code */, resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder = new NotificationCompat.Builder(mContext);
        mBuilder.setSmallIcon(R.mipmap.ic_launcher);
        mBuilder.setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(false)
                .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
                .setContentIntent(resultPendingIntent);

        mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
        {
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
            assert mNotificationManager != null;
            mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        assert mNotificationManager != null;
        mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
answered on Stack Overflow Jan 18, 2019 by Adrián Galfioni

User contributions licensed under CC BY-SA 3.0