3

I am following this walkthrough for creating push notifications into a Xamarin forms app

https://learn.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:

  1. Have I done something wrong?
  2. 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?
  3. 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?
Journeyman1234
  • 547
  • 4
  • 18
  • seems like you need a Service too, check about [Android Service](https://developer.android.com/training/notify-user/build-notification#java) and [Xamarin Android Notification](https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/services/service-notifications) or [Xamarin Notifications](https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/notifications/) – FabriBertani Sep 06 '19 at 19:21
  • I've been through most of those links, that's what I tried to summarise in my post but as I said even the official Xamarin documentation tells you to use a Nuget package for Firebase that's actually been deprecated – Journeyman1234 Sep 07 '19 at 10:09

1 Answers1

0

try to put JSON key 'notification' in your request to firebase API but instead use 'data'

{
 "to": "/topics/journal",
 "notification": {
 "title" : "title",
 "text": "data!",
 "icon": "ic_notification"
  }
}

in this case,these messages trigger the onMessageReceived() callback only when your app is in foreground

{
 "to": "/topics/dev_journal",
 "data": {
   "text":"text",
   "title":"",
   "line1":"Journal",
   "line2":"刊物"
  }
}   

in this case,theses messages trigger the onMessageReceived() callback even if your app is in foreground/background/killed

but this is a topic that depends also on the device and that FCM has no total control over.

the more you could refer to https://stackoverflow.com/a/37845174/10768653

and https://stackoverflow.com/a/43131488/10768653

Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • I am using the Azure web portal to send messages and it defaults to using 'data' as the identifier, so all my tests have used this format. I had also read the other links you have shared, but I do not believe the device I am using is restricting this functionality – Journeyman1234 Sep 09 '19 at 12:32
  • @Journeyman1234 try to add `` `` to manifest xml – Leo Zhu Sep 10 '19 at 01:32
  • Nope, I am afraid that has not worked either. Tested in a release build on device just now, displayes messages in system tray whether app is in foreground or background, but after force stop no more messages arrive until the app is restarted by the user – Journeyman1234 Sep 10 '19 at 08:31
  • @Journeyman1234 this is an issue for many people,so FCM has no total control over. – Leo Zhu Sep 10 '19 at 08:48