Example: Trigger In-App Message from Push Open
Sometimes you may want to trigger a specific in-app message upon opening the app after a push notification is clicked. For example, using a push to alert customers about a new feature or product is available and when the app opens from clicking the push, display an in-app message with more details.
Step 1. Code Setup
Whenever a push notification sent from OneSignal is clicked and opens the app, the OneSignal SDK Notification Opened Event Handler
gets triggered. Within this event handler, the push notification payload is available. This allows you to pass data from the notification into the OneSignal SDK addTrigger
method to show the specific in-app message.
In this example, we set the Notification Opened Event Handler to detect if any additional data is available in the push and if it is pass the data into the addTrigger
method.
The specific data we are looking for starts with the key notification_topic
and we pass this in along with the value to the addTrigger
method.
OneSignal.setNotificationOpenedHandler(new OneSignal.OSNotificationOpenedHandler() {
@Override
public void notificationOpened(OSNotificationOpenedResult result) {
JSONObject data = result.getNotification().getAdditionalData();
Log.i("OneSignalExample", "Notification Data: " + data);
String notification_topic;
if (data != null) {
notification_topic = data.optString("notification_topic", null);
if (notification_topic != null) {
OneSignal.addTrigger("notification_topic", notification_topic);
}
}
}
});
Step 2. In-App Message Setup
Design Your In-App Message as you need.
Once in the Trigger section, set the "In-App Message Trigger" to be notification_topic
is and the value to identify triggering this in-app message. For this example, we set the value to be new feature
.
While testing, make sure to set the Schedule > Advanced Settings > How often do you want to show this message? to be Every time trigger conditions are satisfied and to change this before going live to the frequency you desire.
Set the in-app message live. This will only trigger (show to users) when that specific notification_topic
key and value are passed into the addTrigger
method.
Step 3. Setup the Push Notification to Trigger the In-App Message
When Sending Push Messages, each push can be sent with additional data which are key : value
pairs of data which can be detected within the SDK Notification Event Handlers.
Make sure to set the Additional Data to be the exact values of the Trigger you setup in step 2.
For example, we set the trigger to be notification_topic
is new feature
, we need to set the Additional Data as notification_topic
: new feature
.
Additional Data is under Advanced Settings > Additional Data in the Dashboard or data
property of the Create notification API.
Data is Case Sensitive!
Make sure you spell the triggers and additional data the same or they will not be detected.
For instance,
notification_topic
andNotification Topic
are different.
For instance, if you send the push with additional data: {"iam": "congrats"} and set the In-App Message Trigger to be "iam is congrats". Then your App's Notification Opened Hander can detect the data and pass it into the addTrigger
method to display the message.
Step 4. Send the Push Notification
Now that the code is set, in-app message is live with triggers, and Push Notification additional data matches the triggers, you can send the push. Once you click the push, you should see the in-app message display in your app.
Updated almost 2 years ago