xamarin.formspush-notificationfirebase-cloud-messagingdeviceid

PUSH Notifications in Xamarin Forms with Microsoft.AppCenter


Q: I am using XAMARIN Forms with Microsoft.AppCenter. How do I get the firebase device ID using the AppCenter API for sending push notifications to my mobile clients (Android/IOS) from my backend?

Frontend (Requirements) 1. XAMARIN Forms 2. Microsoft.AppCenter 3. Microsoft.AppCenter.Push 4. MUST use AppCenter to get the FireBase deviceID. 5. AppCenter.GetInstallIdAsync() will not work for my backend. I need the FireBaseDeviceID 6.

Backend (Requirements) 1. I must make the call using FireBase’s https://fcm.googleapis.com/fcm/send POST request.

I have a backend server that needs to send the push directly to firebase. NOT thru AppCenter. I need firebase’s DeviceID for each device I want to send to. I have it working for pushing to all devices for my app. But I also need to go to a specific device that was registered thru AppCenter. This is why I need my front end apps to get the FireBase DeviceID for the PUSH.


Solution

  • You don't have the possibility to get Firebase id from using AppCenter.
    You have two ways out of this:
    1. Skip using AppCenter for pushes in your app. You'd have to implement interaction with Firebase on each platform you support according to documentation. I can share more tips if you choose this option.
    2. Convince backend team to use AppCenter API to send individual push notifications https://openapi.appcenter.ms/#/push/Push_Send
    I know each of these solutions contradicts one of your requirements. But you'd have to choose only one push service, either AppCenter or Firebase, for your project and stick with it. Personally I vote for Firebase, learned it the hard way.

    UPDATED

    Most of the time, Firebase documentation is your best friend when switching to FCM.

    On the app side:

    Setting up Firebase client for iOS and for Android.

    Some tricks from my experience:

    Android:
    You'll have to listen to Firebase InstanceID changes in a descendant of FirebaseMessagingService and store it for later use. It's not available whenever you need it as it is with AppCenter.

    My activity has LaunchMode = LaunchMode.SingleTopand I override two methods to handle push depending on app state when it arrived: void OnNewIntent(Intent intent) and void OnPostCreate(Bundle savedInstanceState). It might not be your case because I handle silent pushes as well. To check if intent contains a push from Firebase run intent?.GetStringExtra("google.message_id") != null.

    iOS:
    Push notifications won't work on iOS simulators and initialization steps might crash your app when run on Simulator. I have created __IOS_SIMULATOR__ constant next to __IOS__ and DEBUG under Debug|iPhoneSimulator configuration in csproj file. And used it in AppDelegate.cs like this:

    #if !__IOS_SIMULATOR__
        new FirebaseCloudMessagingInitializer().Init();
    #endif
    

    AppDelegate offers two methods to override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo) and void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler). I used the first one at the beginning and faced edge cases when it was not called. So I had to switch to overriding the latter one. But don't follow this advice blindly, check what suits best for your needs.

    Also, beware of https://github.com/firebase/firebase-ios-sdk/issues/2438, I'm not sure whether it was already fixed since I had to deal with it. In case it is still there, apply the fix from https://github.com/firebase/firebase-ios-sdk/issues/2438#issuecomment-469472087

    On backend side:

    https://fcm.googleapis.com/fcm/send is so called legacy protocol, use HTTP v1 instead
    POST https://fcm.googleapis.com/v1/projects/project_name/messages:send. Explore documentation.

    How to authorize send request

    How to compose push content