Garbit
Garbit

Reputation: 6036

Xamarin - Oreo (API 26) Firebase Cloud Messaging background notifications not showing in notification drawer

I'm developing an Android application using Visual Studio + Xamarin and trying to receive background Firebase Cloud Messaging notifications on Oreo (API 26).

Problem
When my app is backgrounded, notifications are not displayed in the notification drawer (only on Android API 26 Oreo). However notifications are working when the app is foregrounded.

Comments
The FCM notifications are working on all other build versions < 26, including background and foreground FCM messages (these appear in the notification drawer and I am able to launch my application from a backgrounded state, or if it's foregrounded the app opens the correct intent).


My settings are as follows;

Manifest

Target Android Version: Android 8.0 (API level 26)
Minimum Android Version: Android 4.1 (API level 16)

Android Build Tools

Android SDK Tools 26.1.1
Android SDK Platform-Tools 26.0.2
Android SDK Build Tools 26.0.2

Firebase Cloud Messaging Payload

I'm sending the following FCM payload using the node.js firebase library:

var payload = {
    notification: {
        title: "My Title",
        body: "My body"
    },
    data: {
        type: 'A_CUSTOM_TYPE_VARIABLE'
    }
};

Errors in Xamarin Application Output
When I send an FCM from my server or via the firebase console and the app is in the background I can see that the app receives the FCM but errors before it reaches my code. There is an event output in the application output panel which reads:

[Notification] Use of stream types is deprecated for operations other than volume control
[Notification] See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case

Developer Options Warning Toast

Settings > System > Developer Options > Show notification channel warnings (turn on)  

I've turned on 'Show notification channel warnings' in the developer options and I am now seeing the following error message when I broadcast and FCM message (whilst app is backgrounded):

Image showing developer warning message when sending FCM


Related Stackoverflow posts
- Oreo API 26 notification not displaying - Solution only talks about how you construct your notification. However, the FirebaseMessageReceiever or MainActivity does not even fire in my case.
- Notifications fail to display in Oreo - Looks like there is a bug in the support library.

Upvotes: 1

Views: 1168

Answers (1)

GS_Dan
GS_Dan

Reputation: 113

I realise this is old, but for anyone else who's having issues the documentation has been updated. Judging by the screenshot, your notification channel is null. For Android 26+ you need to create one:

void CreateNotificationChannel()
{
    if (Build.VERSION.SdkInt < BuildVersionCodes.O)
    {
        // Notification channels are new in API 26 (and not a part of the
        // support library). There is no need to create a notification
        // channel on older versions of Android.
        return;
    }

    var channel = new NotificationChannel(MyFirebaseMessagingService.CHANNEL_ID,
                                          "FCM Notifications",
                                          NotificationImportance.Default)
                  {

                      Description = "Firebase Cloud Messages appear in this channel"
                  };

    var notificationManager = (NotificationManager)GetSystemService(Android.Content.Context.NotificationService);
    notificationManager.CreateNotificationChannel(channel);
}

https://learn.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=vsmac#check-for-google-play-services-and-create-a-notification-channel

Upvotes: 1

Related Questions