OnMessageReceived in android O is not called when the application is in the background

I send payload notifications from my server. here is an example:

url= "https://fcm.googleapis.com/fcm/send"
{
  "to" : "userToken",
  "data" : {
    //some json here
  }
}

this way, I successfully send messages to users, even if the application does not work, on all devices prior to Android O. But on the Android O device, onMessageReceived is not called when the application does not start ...

Are there any new rules in O? how can this be fixed? thank!

UPDATES

This question is not about notifications, but about messages about firebase srvice!

Anyway, for Android O are also available:

val CHANEL_ID = "MY_CHANEL_ID"

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val channel = NotificationChannel(CHANEL_ID, "Channel human readable title", NotificationManager.IMPORTANCE_HIGH)
    (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel)
}
+4
source share
3 answers

you need to create a notification channel before it can be used.

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

      /* Create or update. */
      NotificationChannel channel = new NotificationChannel("my_channel_01",
          "Channel human readable title", 
          NotificationManager.IMPORTANCE_DEFAULT);
      mNotificationManager.createNotificationChannel(channel);
  }

, targetSdkVersion 26 .

NotificationCompat.Builder, - : https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0-beta2 ( setChannelId ).

, minSdkLevel 14.

0

Android 8.0 ( API 26), , . , . Android 8.0 ( API 26), . Android 8.0 ( API 26), Android 8.0 ( API 26), Android 7.1 ( API 25) .

https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels

:

        // The id of the channel.
        String CHANNEL_ID = "my_channel_01";
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(MainActivity.this).setChannel(CHANNEL_ID)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!");
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(this, MainActivity.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your app to the Home screen.
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MainActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // mNotificationId is a unique integer your app uses to identify the
        // notification. For example, to cancel the notification, you can pass its ID
        // number to NotificationManager.cancel().
        mNotificationManager.notify(0, mBuilder.build());
0

it looks like the problem was in my one plus hydrogen beta. when I drop it and do a full phone update - it works fine.

0
source

Source: https://habr.com/ru/post/1690193/


All Articles