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.
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
Related
We've been using Shiny framework for Local Notifications and it has done what we needed which is make a notification appear on iOS and Android that when tapped will open the app.
However the OnReceived method on the INotificationDelegate never gets called on iOS. This has been the case forever IIRC but was of no real concern as we didn't need to do anything with the OnReceived except track it in AppCenter. Therefore fixing it never got prioritised.
Recently we have been looking at using Push Notifications. We are not using Shiny's push and instead are registering the device using out our own backend WebApi.
I am seeing a problem where our AppDelegate override of ReceivedRemoteNotification does not get called if i call the UseNotifications extension in our ShinyStartup ConfigureServices. We want to continue using Shiny for Local Notifications so this is a problem.
I am wondering if the issue that means our local notification received delegate does not get called is related to this issue.
You are using ReceivedRemoteNotification which is for the "old" way of catching remote notifications in the foreground. Shiny.Notifications uses a UNUserNotificationCenterDelegate which I believe intercepts and stops that method. You can implement the Shiny notification delegate to work with the notification or use Shiny.Push.AzureNotificationHubs.
You can also use: DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action completionHandler) which is a better method to handle remote notifications anyhow since it is needed for background processing
I had a project from two application(flutter application) and three flutter-web on the same firebase i want to send notification from the web to the application.
i searched alot for this and i cant find any answer or solution for this case.
I had a project from two application(flutter application) and three flutter-web on the same firebase i want to send notification from the web to the application.
i searched alot for this and i cant find any answer or solution for this case.
I hope you understand that we can't give you the finish solution here. I would recommend to you to go trough this docs.
What we can do is to explain it a little bit. For your solution I would recommend to use FCM. And how they work is that:
you use the Firebase SDK to receive a token from the client device.
you store that token on one of the Firebase databases
when sending a push notification to a device you would need to do it from the Firebase Cloud Functions and use that token to send the message to it. When you do that the device from witch the token is will get the message.
It doesn't matter what platform the device is (web, ios or android). All of them can receive Push Messages (in Web most of them but nor 100% all).
From your use case I would not recommend to use topics. You want to send messages from device to device and therefore the tokens are the best solution for it.
I'm needing help. I need to execute a job (send to the server the UserId and the user location) even if the app is closed. I have tried many different approaches but I couldn't make it work.
It's an Xamarin forms app (android & iOS). I'm focused on Android side.
I have a push notification service working (FCM using the azure-NotificationHub) if necessary for the proposed solution.
If you need this task executed at a regular cadence, I would recommend adding a service to your application that will run in the background and largely be separated from the state of the rest of your application. You can find some documentation on that here: Run Background Service - Android.
You mentioned that you have a Notification Hub if necessary - essentially the Android Client SDK for Notification Hubs will be creating a service like the one linked above and listening for events to trigger certain actions. If you need data at random (i.e. not at a regular cadence) this could be a good approach. However, assuming you just want an updated location every few minutes, it's probably simpler to just run your own background service.
I want to implement logic to my xamarin.Forms application (for android and Ios), which sends notification to user when something new appears in database. For example when other user add an invitation to database, invited user will receive it. Thanks
If you want to send out push notifications, take a look at One Signal. It is free to use and in my experience is also the easiest to set up with Xamarin.Forms. You would need to follow their tutorial on how to get things set up for iOS, Android, and your server side implementations. After that when something in your DB changes just send a push notification from your server.
Here is a link to the Xamarin.Forms setup: One Signal - Xamarin.Forms
You can also use them to notify the users via email, however I have not tried that.
It depends completely on your underlying backend and services (REST? Websockets?, ...) and is to general to answer clearly.
For a good instruction how to use notifications in Xamarin.Android, take a look here:
Local notifications on Android
I'm using the following plugin for implementing and handling FCM push notifications in my xamarin application and there's one little unaddressed problem I'm facing.
I was hoping someone else has already figured it out and could help me.
THE PROBLEM:
It seems to be duplicating my MainActivity although I have the LaunchMode set to SingleTop.
There's an event called OnNotificationReceived that's provided by the library.
This event fires as expected, only once, when the application is in the foreground.
When I minimize the application (let it run in the background), send a notification to the device and tap on the notification, it opens the application, as expected, but if I send another notification after that, while the application is in the foreground, The OnNotificationReceived Event is fired twice.
I've tried changing the NotificationActivityFlags property to SingleTop.
I've tried changing my MainActivity's Launch Mode to SingleTop
I tried running the sample application they've provided to check if its something that I'm doing wrong, and still the same thing happens.
Logging the issue in their Github repository
None of the above have worked.
The issue can be reproduced by:
downloading the sample from the repository, link provided above
replacing their google-services.json file with your own from the Firebase
console.
change the package name of the sample of the application to the one you
have registered on Firebase
following the steps above that I've mentioned
ADDITIONAL INFO
Version Number of Plugin: 1.3.0
Device Tested On: Huawei P8 Lite Android Version 6.0
Version of VS: Visual Studio 2019
Version of Xamarin: 3.4.0.1009999
Update:
Unfortunately, the method below has not proven successful after attempting to implement it, I should have thought of the fact that I'm still going to need the notification object for iOS, earlier.
Myself and the team are going to implement a way to keep track of the users platform on the server and send them a notification either with or without the notification based on their platform, since iOS handles FCM notifications differently from Android. For now we're going to use Google's Instance ID service to determine what platform existing users are on.
Workaround:
I hope this helps someone in the future, if not I hope someone can provide me with a better solution.
In order to have my cake and eat it, basically be able to keep the notification object in the payload instead of removing it and having to further customise the payload to display notifications just for iOS, I changed the MainActivity's (the activity thats launched when a user taps on the notification) Launch mode to SingleInstance. That way I can keep my notification object in the payload and not have to worry about the OnNotificationReceived event being triggered twice.