2

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.

Mike
  • 35
  • 6
  • In Short. Does Microsoft.AppCenter expose the FireBase DevceID? Is so, how do get it. – Mike Feb 07 '20 at 21:01

1 Answers1

3

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

foxanna
  • 1,500
  • 16
  • 26
  • Thank you, FYI. The frontend and backend team are me. I do not want to use AppCenter on the backend, so #2 is not an option. As for #1, I guess I'll have to stop using AppCenter for the pushes. It just made things so darn easy to implement. I hope the firebase direction is fairly easy to implement. And yes!!! could you give me some tips on making the switch. – Mike Feb 07 '20 at 21:12
  • I also started implementing pushes with AppCenter and was amazed with the simplicity of the solution. But then I faced limited functionality like not being able to send 'silent' pushes to Android. And also an AppCenter one-time issue, which hit the business I work for pretty hard. So I had to switch to FCM. Unfortunately, it is not as obvious as AppCenter api. – foxanna Feb 07 '20 at 21:20
  • Could you point me to a link(s) that has good and clear steps on making this work using Xamarin Forms. - ура – Mike Feb 07 '20 at 21:34
  • Do you think this video would be helpful for implementing FCM notifications in my Xamarin Forms application for an Android device? Do you think it is heading me into the right direction on doing it correctly? https://www.youtube.com/watch?v=yIZzX3YNFiM – Mike Feb 07 '20 at 22:11
  • That feeling when you realize you should have written that damn push notifications manual long ago :) I have updated my answer with some links and thoughts that randomly crossed my mind when looking through my implementation of push notifications. Hope you find it helpful. – foxanna Feb 07 '20 at 22:27
  • Thank You - I'd up vote ya but stackoverflow still has my training wheels on. I need a reputation of 15 or more :) – Mike Feb 07 '20 at 23:10
  • Welcome and good luck with that beast. Feel free to ping me if you have more questions. – foxanna Feb 07 '20 at 23:16