Forum Xamarin.Forms

Notifications to work on Android if the app is closed, stopped or not running???

faceoffers28faceoffers28 USUniversity ✭✭✭
edited September 2018 in Xamarin.Forms

Does anyone have an elegant solution for this? I know there are certain devices that stop the services when the app is closed, but are there ways to combat this? Also, does anyone know if most Android devices handle Notifications correctly if the app is not running? If so, perhaps it's just a small set of devices.

I'm using Azure Notification Hubs to send Firebase Cloud Messaging Notifications to my Xamarin.Forms Android app. My app targets API 27 and is compiled using API 27. Notifications work great when the app is in the foreground or background. I'm running Xamarin.Firebase.Messaging version 60.1142.1.

Seems like I could start a service when the device reboots or perhaps start a service when the app is closed? Is this doable? The service would have to somehow listen for [IntentFilter(new[] { "" })], extract the message data from the Intent, build a Notification and display it. It would also have to stop running if the app is running so as not to conflict with the app and how it handles Notifications. I've created a couple of test Services, but I can't get the message from the Intent. Perhaps there is a way to start the FirebaseMessagingService on boot or when the app closes?

I've also read conflicting information regarding setting up BroadcastReceiver on device boot. I tried this, but could not get it to work. Here is some sample code. My BroadcastReceiver is trying to get the message from here. [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })]. This was in my OnReceive method. As you can see, I'm trying to extract the message from the Intent and call the OnStartCommand in MyService.

Intent intent2 = new Intent(Application.Context, typeof(MyService));
intent2.PutExtra("message", message);
intent2.PutExtra("title", title);
intent2.PutExtra("id", id);


This is one of the more confusing and poorly documented problems I've encountered, which is why I'm asking for help. Thanks in advance! Any assistance is greatly appreciated.

Best Answer


Sign In or Register to comment.