I am following this walkthrough for creating push notifications into a Xamarin forms app
https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-android-push-notification-google-fcm-get-started
I have successfully integrated with NotificationHub and can receive test messages on the device when app is in focus or in background
I cannot, however, manage to make the app wake in order to receive messages, which is the main reason for having notifications
There are a great many posts about this issue and I have spent the afternoon reading a lot of them, but I have yet to find anything that works, even when others insist they do.
I am building on a Motorola device, not an emulator
My manifest looks, in part, like this:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver" android:exported="false" />
<receiver android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
The SendNotification method - I modified the example code to enable it to display in the Android UI, which it does
var intent = new Intent(this, typeof(MainActivity));
intent.AddFlags(ActivityFlags.ClearTop);
intent.PutExtra("message", body);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, Authenticate.Constants.NotificationChannelName)
.SetContentTitle("XamarinNotify Message")
.SetSmallIcon(Resource.Drawable.ic_launcher)
.SetContentText(body)
.SetAutoCancel(true)
.SetShowWhen(false)
.SetContentIntent(pendingIntent);
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
notificationBuilder.SetChannelId(Authenticate.Constants.NotificationChannelName);
}
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(0, notificationBuilder.Build());
// Awesome - this displays the message in the UI but only when the app is running
var notificationManagerCompat = NotificationManagerCompat.From(this);
notificationManager.Notify(0, notificationBuilder.Build());
However, if I force stop the app, the notification will never arrive until the app is restarted manually
I do not know:
- Have I done something wrong?
- If I need to implement a service of some kind, what is it? Something that would wake the app? I looked into having a Worker with a PeriodicWorkRequest but then couldn't determine if this was the right approach and if so what am I looking to have it call when the timer runs?
- I also looked into BroadcastReceiver but some of the packages used in the example on the Microsoft site have now been deprecated, like WakefulBroadcastReceiver, which lead me to investigate 2?