Plugin.Firebase.CloudMessaging 3.0.0

dotnet add package Plugin.Firebase.CloudMessaging --version 3.0.0
NuGet\Install-Package Plugin.Firebase.CloudMessaging -Version 3.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Plugin.Firebase.CloudMessaging" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Plugin.Firebase.CloudMessaging --version 3.0.0
#r "nuget: Plugin.Firebase.CloudMessaging, 3.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Plugin.Firebase.CloudMessaging as a Cake Addin
#addin nuget:?package=Plugin.Firebase.CloudMessaging&version=3.0.0

// Install Plugin.Firebase.CloudMessaging as a Cake Tool
#tool nuget:?package=Plugin.Firebase.CloudMessaging&version=3.0.0

Cloud Messaging

Firebase Cloud Messaging offers a broad range of messaging options and capabilities. I invite you to read the following documentation to have a better understanding about notification messages and data messages and what you can do with them using FCM's options.

Installation

Nuget

NuGet

Install-Package Plugin.Firebase.CloudMessaging

Setup

  • Follow the instructions for the basic setup
  • Enable Cloud Messaging at your project in the Firebase Console
  • Add the following line of code after calling CrossFirebase.Initialize():
#if IOS
  FirebaseCloudMessagingImplementation.Initialize();
#endif
  • Make sure the device is able to receive cloud messages and the user has granted the permissions for it:
  CrossFirebaseCloudMessaging.Current.CheckIfValid()

iOS specifics

  <key>aps-environment</key>
  <string>development</string>
  • For testing launch the app without the debugger, otherwise the push notification may not be received
  • For more specific instructions take a look at the official Firebase documentation

Android specifics

  • Add the following code snippet to the <application> tag in your apps AndroidManifest.xml:
  <receiver
    android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
    android:exported="false" />
  <receiver
    android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
    android:exported="true"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>
      <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
      <category android:name="${applicationId}" />
    </intent-filter>
  </receiver>
  • Call FirebaseCloudMessagingImplementation.OnNewIntent(intent) from MainActivity.OnCreate(...) and MainActivity.OnNewIntent(...)
  • Create a notification channel and set the ChannelId to FirebaseCloudMessagingImplementation:
  private void CreateNotificationChannel()
  {
      var channelId = $"{Application.Context.PackageName}.general";
      var notificationManager = (NotificationManager) Application.Context.GetSystemService(Context.NotificationService);
      var channel = new NotificationChannel(channelId, "General", NotificationImportance.Default);
      notificationManager.CreateNotificationChannel(channel);
      FirebaseCloudMessagingImplementation.ChannelId = channelId;
  }
Customize local push notifications

Local push notifications are shown by default in a specific way, but you have the following two options to customize this behavior:

Overriding FirebaseCloudMessagingImplementation.NotificationBuilderProvider:
FirebaseCloudMessagingImplementation.NotificationBuilderProvider = notificaion => new NotificationCompat.Builder(context, channelId)
    .SetSmallIcon(Android.Resource.Drawable.SymDefAppIcon)
    .SetContentTitle(notification.Title)
    .SetContentText(notification.Body)
    .SetPriority(NotificationCompat.PriorityDefault)
    .SetAutoCancel(true);
Setting FirebaseCloudMessagingImplementation.ShowLocalNotificationAction:
FirebaseCloudMessagingImplementation.ShowLocalNotificationAction = notification => {

    var intent = PackageManager.GetLaunchIntentForPackage(PackageName);
    intent.PutExtra(FirebaseCloudMessagingImplementation.IntentKeyFCMNotification, notification.ToBundle());
    intent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);

    var pendingIntent = PendingIntent.GetActivity(context, 0, intent, PendingIntentFlags.Immutable | PendingIntentFlags.UpdateCurrent);
    var builder = new NotificationCompat.Builder(context, channelId)
        .SetSmallIcon(Android.Resource.Drawable.SymDefAppIcon)
        .SetContentTitle(notification.Title)
        .SetContentText(notification.Body)
        .SetPriority(NotificationCompat.PriorityDefault)
        .SetAutoCancel(true);

    var notificationManager = (NotificationManager) GetSystemService(NotificationService);
    notificationManager.Notify(123, builder.SetContentIntent(pendingIntent).Build());
};

You can find more specific instructions for android at the official Firebase documentation

Usage

Take a look at the documentation for the AdamE.Firebase.iOS.CloudMessaging packages, because Plugin.Firebase's code is abstracted but still very similar.

Since code should be documenting itself you can also take a look at the following classes:

Test with curl

curl --location 'https://fcm.googleapis.com/fcm/send' \
--header 'Authorization: key=<your-api-token-from-firebase-console-cloud-messaging-project-settings>' \
--header 'Content-Type: application/json' \
--data '{
 "to" : "<your-device-fcm-token>",
 "collapse_key" : "type_a",
 "mutable_content": true,
 "notification" : {
     "title": "Knock knock",
     "body" : "Who's there?",
     "badge": 1
 },
 "data" : {
     "body" : "body of your notification in data",
     "title": "title of your notification in data",
     "is_silent_in_foreground": "false"
 }
}'
Extra flags
is_silent_in_foreground

Add "is_silent_in_foreground": "true" to the data payload to prevent showing the local push notification. The flag prevents a notification to be generated if the app is in foreground - otherwise the notifications will still be shown. Event CrossFirebaseCloudMessaging.Current.NotificationReceived is still triggered to allow for manual notification handling.

Note: this is a Plugin.Firebase custom field and hence not documented with google FCM documentation.

Troubleshooting

If you are having trouble receiving push notifications on your device, take a look at this helpful https://github.com/TobiasBuchholz/Plugin.Firebase/issues/145#issuecomment-1455182588 by @andyzukunft. Additionally he has created a dedicated project to simplify the demonstration on how Firebase Cloud Messaging works: https://github.com/andyzukunft/Plugin.Firebase/tree/fcm-demo/sample/Fcm

Release notes

  • Version 3.0.0
    • Swapped Xamarin.Firebase.iOS.CloudMessaging (native SDK 8.10.0) for AdamE.Firebase.iOS.CloudMessaging (native SDK 10.24.0)
  • Version 2.0.4
    • Add FirebaseCloudMessagingImplementation.ShowLocalNotificationAction (issue #163)
  • Version 2.0.3
    • Enable silent push notifications when the app is in foreground (PR #188)
  • Version 2.0.2
    • Prevent error message when no image is attached to push notifications
  • Version 2.0.1
    • Remove unnecessary UseMaui property from csproj files
    • Readd net6.0 tfm
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-android31.0 is compatible.  net6.0-ios was computed.  net6.0-ios16.1 is compatible.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on Plugin.Firebase.CloudMessaging:

Package Downloads
Plugin.Firebase

The plugin includes cross-platform APIs for Firebase Analytics, Auth, Cloud Messaging, Crashlytics, Dynamic Links, Firestore, Cloud Functions, Remote Config and Storage.

MetaFrm.Maui.Essentials.net7.0

Meta Framework (Multi platform & Meta management)

MetaFrm.Maui.Essentials.net8.0

Meta Framework (Multi platform & Meta management)

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 4,046 5/29/2024
2.0.4 43,384 11/20/2023
2.0.3 29,432 7/28/2023
2.0.2 21,954 4/11/2023
2.0.1 1,987 3/31/2023
2.0.0 3,313 3/26/2023