Push Notification Integration Guide

Notification Payload Structure

Push notifications are sent with a structured payload designed to provide comprehensive information for each notification. Below is an overview of what is included in the payload and the significance of each field:

Data Payload

{
  "message": {
    "token": "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "data": {
      "notification_id": "unique_notification_identifier",
      "sender": "b.well",
      "receiver": "intended_recipient",
      "notification_type": ["new_record", "new_insight"],
      "title": "notification_title",
      "body": "notification_body_text",
      "action_type": "deeplink",
      "action": "action_link_or_url"
    }
  }
}
  • notification_id: A unique identifier for tracking and analytics.
  • sender: The origin of the notification.
  • receiver: The intended recipient type within the app.
  • notification_type: Categorizes the notification for appropriate handling.
  • title: The title of the notification for display.
  • body: The detailed message content of the notification.
  • action_type: Indicates the type of action the notification triggers.
  • action: The actual link associated with the action (deep link or URL).

Handling Notifications

To ensure correct handling of notification data, particularly for tracking user interactions with system tray notifications, a potential process is outlined below:

1. Modify/Extend FirebaseMessagingService

Extract the entire data map from the RemoteMessage object, serialize it, and add it as an extra in the Intent used to launch the notification.

override fun onMessageReceived(remoteMessage: RemoteMessage) {
...
    if (remoteMessage.data.isNotEmpty()) {
        val notificationIntent = Intent(this, YourTargetActivity::class.java).apply {
            // serialize the data map to a JSON string and add it as an extra
            putExtra("remoteMessageData", JSONObject(remoteMessage.data).toString())
        }
    }
}

2. Modify/Extend AppCompatActivity

Listen to when an activity is shifted to the foreground and detect if intent data has been injected via onMessageReceived.

override fun onResume() {
...
super.onResume()
val remoteMessageData = intent.getStringExtra("remoteMessageData")
if (!remoteMessageData.isNullOrBlank()) {
    val remoteMessageDataObject = JSONObject(remoteMessageData)
    val action = remoteMessageDataObject.getString("action")
    // Execute any sort of action such as navigating to a deep link
    CoroutineScope(Dispatchers.IO).launch {
        // Register notification was received by end user
        BWellSdk.event.handleNotification(remoteMessageData)
    }
}
}

Testing Notifications

Using cURL Command

You can trigger a push notification by making a POST request to the Iterable API. Here’s how to do it:

curl -X POST \
  https://api.iterable.com/api/push/target \
  -H 'Api-Key: XXXX' \
  -H 'Content-Type: application/json' \
  -d '{
        "campaignId": 8926938, // Replace with appropriate campaignId
        "recipientUserId": "USER_ID" // Replace with the b.well master person id
      }'

Replace XXXX with your Iterable API key and USER_ID with the b.well master person ID of the intended recipient.

Through Iterable API Documentation

Alternatively, you can submit API requests directly through Iterable’s API documentation:

  • Navigate to Iterable API Docs.
  • Find the /api/push/target endpoint.
  • Enter your API key and the following request body:
{
  "campaignId": 8926938, // Replace with appropriate campaignId
  "recipientUserId": "USER_ID" // Replace with the b.well master person id
}

Key Points

  • Ensure you have the correct campaignId and recipientUserId.
  • Use the provided payload structure to understand the context and purpose of each notification.
  • The notificationId is particularly important for tracking and analytics purposes.

By following these steps, you can effectively trigger and manage push notifications for your users, leveraging the structured payload for optimal user engagement and analytical insights.