Firebase-Cloud-Messaging-for-Xamarin-Forms-Android-and-iOS
So many people have asked for convenient and efficient way of implementing FCM (Firebase Cloud Messaging) for android and iOS in Xamarin Form. Actually FCM library cannot be installed in xamarin forms due to version conflict of android support library present in xamarin forms. So here is the work around
Firebase Cloud Messaging (FCM) is a cross-platform service that handles the sending, routing, and queueing of messages between server applications and mobile client apps. FCM is the successor to Google Cloud Messaging (GCM), and it is built on Google Play Services.
Getting Firebase Ready
Android Firebase Cloud Messaging Setup
- Login to https://console.firebase.google.com
- Create and name project, the project name does not matter much at this point and can be anything you want.
- We will start with Android but iOS is the same up to this point. The Package name must match your build environment.
- Click Continue and Finish and let Firebase build your app.
- Once it's ready go to your Android app settings.
- Click on Cloud Messaging at the top.
- Under Project credentials you need to save your Sender ID as that is required for setting up Android.
iOS Firebase Cloud Messaging Setup
- Setup a second app (or first if iOS only).
- Enter the iOS bundle ID this has to match the project you are going to set up.
- Download the GoogleService-Info.plist as you will need this later in your project, it can be downloaded later under app settings on Firebase.
- Click Continue then Finish. Go to the iOS app settings then Cloud Messaging from the top Menu. You will see two places to upload APN certificates, one for Development and one for Production.
- The easiest way to get App ID Identifiers, Provisioning Profiles, and Certificates set up is to let XCode handle the setup. Open XCode and create a Single View app and give it the iOS bundle ID from before. In XCode if you want the Bundle Identifier of com.fcm.sample you give the Product Name sample and set Organization Identifier to com.fcm
- Once the app opens change the team to yours and let XCode setup your certificates.
- Under Capabilities turn Push Notifications on
- Further down turn on Background Modes and enable Remote notifications
- Log on to https://developer.apple.com, click on Certificates, IDs & Profiles, Under Identifiers click App IDs you will see an ID for your app with a name of XC com fcm sample and an ID of com.fcm.sample. Click on the ID you are trying to configure with push.
- Click Edit then Create Ceritificate for your Development SSL Certificate
- You will need to generate a Certificate Signing Request, open Keychain Access on your Mac, Click Keychain Access > Certificate Assistand > Request a Certificate From a Certificate Authority...
- Fill out the information and Save to Disk
- Upload your CSR to Apple, you can use the same CSR for both Production and Development.
- Download one or both certs and open them with Keychain Access. They can be found under My Certificates once opened.
- The certs will tell you if they are dev or production, expand them and export the Private Key, Give them a strong password, and a proper name.
- Upload Certificates to Firebase iOS app and enter the password you created.
Done Getting Firebase Ready
Xamarin Forms Android Setup and Info
I have used the following project for GCM.Client implementation https://github.com/Redth/GCM.Client
If you want a similar implementation in your project then copy the GCM.Client folder in your project
In MainActivity.cs class declare
using Gcm.Client; at the top
and in class body
static MainActivity instance = null;
// Return the current activity instance.
public static MainActivity CurrentActivity
{
get
{
return instance;
}
}
In OnCreate method
instance = this;
try
{
// Check to ensure everything's set up right
GcmClient.CheckDevice(this);
GcmClient.CheckManifest(this);
// Register for push notifications
System.Diagnostics.Debug.WriteLine("Registering...");
GcmClient.Register(this, PushHandlerBroadcastReceiver.SENDER_IDS);
}
catch (Java.Net.MalformedURLException)
{
CreateAndShowDialog("There was an error creating the client. Verify the URL.", "Error");
}
catch (Exception e)
{
CreateAndShowDialog(e.Message, "Error");
}
Declare the function
private void CreateAndShowDialog(String message, String title)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetMessage(message);
builder.SetTitle(title);
builder.Create().Show();
}
Your app needs to import Gcm.Client folder into the project
In that folder following classes are present
Constants.cs //Constants for GCM. In that file you also need to provide SENDER_ID as derived from your firebase console application
https://console.firebase.google.com/
GcmBroadcastReceiverBase.cs //Receiver for push notification
GcmService.cs //A service for registering and receiving notification
GcmServiceBase.cs //Base class
InternalGcmClient.cs //Utility class for GCM Client
Following nuggets needs to be present in your application
Xamarin.Android.Support.v7.AppCompat 23.3.0
Xamarin.GooglePlayServices.Gcm 29.0.0.2
After doing all those tasks above, your android project is ready to process push notification fired by firebase console
Xamarin Forms iOS Setup and Info
- Add Package Firebase APIs Cloud Messaging iOS Library
- Make sure info.plist Matches your iOS Bundle ID for Bundle Identifier the Application Name is what the user will see for your installed app.
- In the info.plist enable Background Modes and enable Remote notifications and in Entitlements.plist enable Push Notifications
- In Project > FCM.iOS Options > Build > iOS Bundle Signing set Provisioning Profile to iOS Team Provisioning Profile: com.fcm.sample or to the Profile for your *iOS Bundle ID
- Add your GoogleService-Info.plist to your project and remove the old one, it's only for an example, Right Click go to Build Action > and check BundleResource
When the App starts up it will attempt to register by first asking the user if the app can send Push messages to them, then depending if it's running on iOS >=9 or iOS 10 it will attempt to register to "/topics/all". If the app is open it will alert the user in app. If the app is closed it will display the message like a normal push.
Out of app message.
In app message.
Regards,
Habib Ali
Mohammad Samiullah Farooqi
Stephen Barker