4

I am sending data payload notifications from my server. here is example:

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

in such way i am successfully sending messages to users, even if app isn't running, in all pre Android O devices. But on Android O device, onMessageReceived not called when app is not launched...

is there some new rules in O ? how it can be fixed? thanks!

UPDATES

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

Anyway, chanels for Android O is also implemented:

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)
}
Andriy Antonov
  • 1,360
  • 2
  • 15
  • 29

3 Answers3

0

you need to create the 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);
  }

Also you need to use channels only if your targetSdkVersion is 26 or higher.

If you are using NotificationCompat.Builder, you also need to update to the beta version of the support library: https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0-beta2 (to be able to call setChannelId on the compat builder).

Be careful as this library update raises minSdkLevel to 14.

Mayank Garg
  • 1,284
  • 1
  • 11
  • 23
0

Starting in Android 8.0 (API level 26), notification channels allow you to create a user-customizable channel for each type of notification you want to display. Notification channels provide a unified system to help users manage notifications. When you target Android 8.0 (API level 26), you must implement one or more notification channels to display notifications to your users. If you don't target Android 8.0 (API level 26) but your app is used on devices running Android 8.0 (API level 26), your app behaves the same as it would on devices running Android 7.1 (API level 25) or lower.

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

Sample Code:

        // 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());
fida1989
  • 3,234
  • 1
  • 27
  • 30
-1

it looks like the problem was in my one plus hydrogen beta. when I drop it and make total refresh of the phone - it works ok.

Andriy Antonov
  • 1,360
  • 2
  • 15
  • 29