Data format in .apns file for testing firebase notification in iOS simulator

Viewed 436

As some of you may know, Xcode version 11.4+ finally support testing notifications inside simulator. We can test by dragging an .apns file to simulator, or running the below command:

xcrun simctl push <DEVICE_ID/NAME> ./notification-test.apns

The structure of .apns file will be as following:

{
    "Simulator Target Bundle": "com.example.app",
    "aps": {
        "alert": {
            "title": "Testing notification with APN file",
            "body": "Click this message to open the app!"
        },
        "badge": 1,
        "mutable-content": 1,
        "sound": "default"
    }
}

Now I am testing my React Native app which listen to notification from firebase messaging.

I would like to test the data inside notification message but is not working.

How could I achieve a testing notification simulating as from firebase messaging, so that messaging().getInitialNotification() or other firebase functions could catch?

1 Answers

After doing some research, you could amend the .apns file like this:

{
    "Simulator Target Bundle": "com.example.app",
    "aps": {
        "alert": {
            "title": "Testing notification with APN file",
            "body": "Click this message to open the app!"
        },
        "badge": 1,
        "mutable-content": 1,
        "sound": "default"
    },
    "someKey": "someValue",                                    // Put any data on this level
    "gcm.message_id": "0:11111111111111111111111111111111"     // Add this line
}

so that you could test and catch notification's data with firebase messaging functions like this:

let notification = await messaging().getInitialNotification();
console.log("notification data", notification.data.someKey); // someValue
Related