Xamarin Forms Android not receiving Push notificat

2020-04-20 06:40发布

问题:

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:

  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?

回答1:

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