Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase cloud messaging stopped with upgrade to Oreo

I was just getting started with Android development and managed to get Firebase messaging working after running the wizard. I was able to receive background notifications on my Nexus 5X running Nougat. But then my 5X upgraded to Oreo and Firebase notifications haven't worked since. I've heard about background execution limitations, but since I'm just getting started I don't know what I actually have to do to get it working again. Are there any write ups on this? I tried a new project from scratch hoping the wizard had been updated, but no change. I was using application broadcast messages and topic subscription messages, no device token used.

like image 857
Steve Prior Avatar asked Oct 09 '17 02:10

Steve Prior


2 Answers

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.

If you change your target version into Android 8.0 (API level 26), you have to do the following change

      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     String id = "id_product";//This will be enter when creating Builder
     // The user-visible name of the channel.
     CharSequence name = "Product";
     // The user-visible description of the channel.
     String description = "Notifications regarding our products";
     int importance = NotificationManager.IMPORTANCE_MAX;
     NotificationChannel mChannel = new NotificationChannel(id, name, importance);
     // Configure the notification channel.
     mChannel.setDescription(description);
     mChannel.enableLights(true);
     // Sets the notification light color for notifications posted to this
     // channel, if the device supports this feature.
     mChannel.setLightColor(Color.RED);
     notificationManager.createNotificationChannel(mChannel);
  }

Change your Builder constructor

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(applicationContext, **ID you have put in the Notification Channel**)

Finally you can create the notification from the Builder

notificationManager.notify("0", notificationBuilder.build())

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

like image 70
dpvmani Avatar answered Sep 28 '22 01:09

dpvmani


Please use a recent version of the FCM sdk.

FCM sdk introduced support for Android O in April, if you are using older version your app would not receive messages in Android O and could potentially crash.

See the latest release here: https://firebase.google.com/support/release-notes/android

PS: firebase SDK has been moved to the Google Maven repository.
Be sure to check out the latest instructions here: https://firebase.google.com/docs/android/setup#manually_add_firebase

like image 22
Diego Giorgini Avatar answered Sep 28 '22 00:09

Diego Giorgini