Receiving Push Notifications

If you use the No-SDK solution to send your Device Tokens to OptiMobile you will need to implement additional client side code to receive and make use of the advanced features of push notifications.

iOS

The OptiMobile API will generate notifications to APNS (Apple Push Notifications Service) in the standard format documented in the Apple developer docs.

Additional OptiMobile extensions will be contained within a section keyed "custom" for example:

{
    "aps": {
        "alert": {
            "title": "notification title",
            "body": "notification message"
        },
        "mutable-content": 1
    },
    "custom": {
        "u": "https://hello.com", // Filled if the URL field from the template is used
        "a": {
            "k.message": {
                "type": 1,
                "data": {
                    "id": 8 // Your OptiMobile messageID, this should be used in any subsequent metrics API calls
                }
            },
            "k.buttons": [
                {
                    "id": "button-1",
                    "text": "Button 1",
                    "icon": {
                        "type": "system",
                        "id": "stuff"
                    }
                },
                {
                    "id": "button-2",
                    "text": "Button 2"
                }
            ],
            "badge_inc": 5,
            // Additional data from the "Include JSON" field from the template editor will be merged here
            "key": "value",
            "key2": "value"
        }
    },
    "attachments": {
    	"pictureUrl": "https://media-cdn.app.delivery/path-to-image.png"
    }
}

Service Extension

Some iOS features also require a Content Service extension in order to modify the notification prior to rendering as documented in the Apple developer docs.

OptiMobile will append image attachments which must be downloaded by the content service extension if the template picture option is used.

Additionally, dynamically added action buttons can be defined on OptiMobile Push notification templates which will be appended to the custom payload shown above and must be processed by the service extension prior to rendering the notification, a code example of handling these buttons is available in our Swift SDK source code.

Finally if the template has made use of incrementing counters for your app's notification badge this will be provided as the badge_inc value, in order to update the badge correctly your app will need to store the existing value locally and apply changes as received from notifications.

Android

In order to receive and display remote notifications on Android you must implement Firebase Cloud Messaging, which requires a Broadcast Receiver and a Firebase Messaging Service implementation.

Please refer to the Firebase Documentation for details.

The format that OptiMobile push notifications, delivered by FCM is as follows:

{
    "message": {
        "data": {
            "title": "notification title",
            "alert": "notification message",
            "bgn": "0",
            "custom": {
                "a": {
                    "k.message": {
                        "type": 1,
                        "data": {
                            "id": 8 // Your OptiMobile messageID, this should be used in any subsequent metrics API calls
                        }
                    },
                    "k.channel": "channel name",
                    "k.notificationType": "important",
                    "k.buttons": [
                        {
                            "id": "button-1",
                            "text": "Button 1"
                        },
                        {
                            "id": "button-2",
                            "text": "Button 2"
                        }
                    ],
                    // Additional data from the "Include JSON" field from the template editor will be merged here
                    "key": "value",
                    "key2": "value"
                },
                "u": "https://hello.com" // Filled if the URL field from the template is used
            }
        },
        "android": {
            "ttl": "600s",
            "priority": "high",
            "collapse_key": "456"
        }
    }
}

For further code examples, a reference can be found in our Android SDK Source code.