简体   繁体   中英

Trouble setting push notification badge for Android and iOS using Xamarin Forms

I'm having trouble with the icon badge using push notifications on my Xamarin Forms app. I'm using Firebase to send the app to both devices.

The way I had the badge working for android was that the app gets the complete notification info from the payload's 'data' section. When the notification only has a data section I can trigger a function that will set the badge and show the popup notification. If I put the same info in the 'notification' section I can't cause a trigger to set the badge.

For iOS, the badge count gets sent from the payload's 'notification' section. iOS doesn't see the 'data' section and create an automatic popup. I've tried triggering a function when the app is in the background but couldn't find a way.

Bottom line: iOS needs the 'notification' payload and Android needs the 'data' payload to show the proper badge.

Does anyone know how I can trigger a function in my AppDelegate on iOS when the app is in the background and a push notification is received and/or does anyone know how to trigger a function in Android when there is a 'notification' section in the payload?

This is my payload:

{
    "to":"{mytoken}",
    "notification":{
        "title":"Appointment Almost Booked",
        "body":"Please confirm your appointment",
        "badge":3
    },
    "data":{
        "title":"Appointment Almost Booked",
        "body":"Please confirm your appointment slot",
        "badge":3
    }
}

For iOS:

You can refer to:

https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support .

In table 1,there is a property content_available you can set to awake your application when your application is in background.

On iOS, use this field to represent content-available in the APNs payload. When a notification or message is sent and this is set to true, an inactive client app is awoken, and the message is sent through APNs as a silent notification and not through the FCM connection server. Note that silent notifications in APNs are not guaranteed to be delivered, and can depend on factors such as the user turning on Low Power Mode, force quitting the app, etc. On Android, data messages wake the app by default. On Chrome, currently not supported.

Then you can trigger your function in DidReceiveRemoteNotification .

And here is a link that might help you:

https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-xamarin-forms-get-started-push#configure-and-run-the-ios-project-optional

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM