Skip to content
Many peoples are concerned about adding FCM to their android project under Xamarin Forms. The firebase library doesn't get installed under xamarin forms due to version issue of Android support lib in xamarin forms. So here is the workaround
C#
Branch: master
Clone or download
Pull request Compare This branch is even with HabibAli:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
FCM
ServerSide/MessageSender
.gitignore
LICENSE
README.md

README.md

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

  1. Login to https://console.firebase.google.com

Create New Project

  1. Create and name project, the project name does not matter much at this point and can be anything you want.

Creating Project

  1. We will start with Android but iOS is the same up to this point. The Package name must match your build environment.

Create Android

  1. Click Continue and Finish and let Firebase build your app.
  2. Once it's ready go to your Android app settings.

Android Settings

  1. Click on Cloud Messaging at the top.

Firebase Menu

  1. Under Project credentials you need to save your Sender ID as that is required for setting up Android.

Project Credentails

iOS Firebase Cloud Messaging Setup


  1. Setup a second app (or first if iOS only).

iOS app setup

  1. Enter the iOS bundle ID this has to match the project you are going to set up.

iOS Bundle ID setup

  1. Download the GoogleService-Info.plist as you will need this later in your project, it can be downloaded later under app settings on Firebase.

Info.plist download

  1. 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.

Firebase APN push certs

  1. 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

xcode setup

  1. Once the app opens change the team to yours and let XCode setup your certificates.

XCode Team setup

  1. Under Capabilities turn Push Notifications on

Turn Push ON

  1. Further down turn on Background Modes and enable Remote notifications

Background Mode ON

  1. 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.

Apple Setup

  1. Click Edit then Create Ceritificate for your Development SSL Certificate

Apple Cert Setup

  1. 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...

Keychain get CSR

  1. Fill out the information and Save to Disk

CSR Save to Disk

  1. Upload your CSR to Apple, you can use the same CSR for both Production and Development.

Cert upload

  1. Download one or both certs and open them with Keychain Access. They can be found under My Certificates once opened.

My Certs

  1. 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.

Dev export

Prod export

Password strong

Name cert

  1. Upload Certificates to Firebase iOS app and enter the password you created.

Firebase upload certs

Firebase upload password

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


  1. Add Package Firebase APIs Cloud Messaging iOS Library

Package to add

  1. 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.

Xamarin iOS bundle setup

  1. In the info.plist enable Background Modes and enable Remote notifications and in Entitlements.plist enable Push Notifications

Background Modes

  1. 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

xamarin profile iOS profile setup

  1. 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

GoogleService plist

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. Out Of App

In app message. In App

Regards,

Habib Ali

Mohammad Samiullah Farooqi

Stephen Barker

You can’t perform that action at this time.