Plugin.Firebase.CloudMessaging
3.1.0
See the version list below for details.
dotnet add package Plugin.Firebase.CloudMessaging --version 3.1.0
NuGet\Install-Package Plugin.Firebase.CloudMessaging -Version 3.1.0
<PackageReference Include="Plugin.Firebase.CloudMessaging" Version="3.1.0" />
paket add Plugin.Firebase.CloudMessaging --version 3.1.0
#r "nuget: Plugin.Firebase.CloudMessaging, 3.1.0"
// Install Plugin.Firebase.CloudMessaging as a Cake Addin #addin nuget:?package=Plugin.Firebase.CloudMessaging&version=3.1.0 // Install Plugin.Firebase.CloudMessaging as a Cake Tool #tool nuget:?package=Plugin.Firebase.CloudMessaging&version=3.1.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
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
- Go to developers.apple.com → Certificates, Identifiers & Profiles → enable Push Notifications in your provisioning profile, download and double tap it
- Create and upload APNs authentication key to your project in the Firebase Console
- Enable Push Notifications in your apps
Entitlements.plist
:
<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 appsAndroidManifest.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)
fromMainActivity.OnCreate(...)
andMainActivity.OnNewIntent(...)
- Create a notification channel and set the
ChannelId
toFirebaseCloudMessagingImplementation
:
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-android34.0 is compatible. net8.0-browser was computed. net8.0-ios was computed. net8.0-ios18.0 is compatible. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Plugin.Firebase.Core (>= 3.1.0)
-
net8.0-android34.0
- Plugin.Firebase.Core (>= 3.1.0)
- Xamarin.Firebase.Messaging (>= 123.0.8)
-
net8.0-ios18.0
- AdamE.Firebase.iOS.CloudMessaging (>= 10.24.0)
- Plugin.Firebase.Core (>= 3.1.0)
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.