question

Daniel Keele avatar image
Daniel Keele asked

Send and read data from notification

Hi there, I'm using `server.SendPushNotification` in CloudScript for my iOS game in Unity. I'm interested in passing data along with my notification, and reading that data in Unity if the player opens the game by tapping on the notification. Any ideas or pointers appreciated. Thank you!

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Daniel Keele avatar image
Daniel Keele answered

I found one solution, but it's clunky. I'm open to any other solutions.

As of the date of writing, Unity's provided way of handling custom data on notifications in-game is the `data` attribute on their iOSNotification class (part of the "Mobile Notifications" package in package manager).

In order to populate the `data` attribute, Unity searches the notification payload for a `data` key, which should be sibling to the `aps` key.

From Unity's docs:
"You have to use the exact data as the key of your custom data, because this is what the package looks for."

{
    "aps": {
        "alert": {
            "title": "Hello world!",
            "body": "This is an example of a remote notification"
            }
    },
    "data": "Test data"
}


Unfortunately, `SendPushNotification`'s API provides no way to write to the `data` key. The closest I could get was using `AdvancedPushPlatformMsg`'s `Json` attribute, but this only lets you write children of `aps`, not siblings.

The only work around I could find was to use Playfab's push notification templates, which let you pass in the entire payload. This is a serious limitation. I'm forced to create a new template any time I want to pass in different custom data.

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Rick Chen avatar image Rick Chen ♦ commented ·

I am glad that you have found a solution yourself. Unfortunately, the server.SendPushNotification only supports `aps` key. Custom key/value pairs are not supported. You can refer to this thread: https://community.playfab.com/questions/46400/push-notification-advancedplatformdelivery-iospush.html. If you would like the feature to add Custom key/value pairs in push notification, please feel free to create a thread in the Feature Request forum.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.