Handling button clicks
Implementation required for interactive mobile push notifications
With the push category capability you can add multiple buttons to your messages. It's then required to handle different buttons clicks. It could be done using our message response handler.
iOS
XPush.registerContinousMessageResponseHandler({(_ response: XPMessageResponse, _ callback: XPMessageCompletionBlock) -> Void in
if response.action.type == XPActionType_Click {
//like button which runs in background
if(response.action.identifier == "like"){
likeArticle(response.message.data["articleId"])
}
//View button which runs in foreground
else if(response.action.identifier == "view"){
openArticle(response.message.data["articleId"])
}
}
//if nothing to be done
completionHandler()
})
[XPush registerMessageResponseHandlerWithCompletion:^(XPMessageResponse * _Nonnull x, XPMessageCompletionBlock _Nonnull completionHandler) {
if (response.action.type == XPActionType_Click) {
switch (response.action.identifier) {
// Like button which runs in background
case "like":
// Like article asynchronously and call completionHandler when finished
[self likeArticle: response.message.data[@"articleId"] completionHandler: completionHandler];
return;
// View button which runs in foreground
case "view":
// Navigate to the article page
[self openArticle: response.message.data[@"articleId"]];
completionHandler();
return;
}
}
// If nothing to be done
completionHandler();
}];
Android
Implement the MessageResponseListener
in your Application.java class
public class YOUR_APPLICATION_CLASS extends Application implements MessageResponseListener {
@Override
public void messageResponseReceived(Message messagePayload,
HashMap<String, String> responsePayload,
WeakReference uiReference) {
if(responsePayload.get("responseType").equals("click")){
switch (responsePayload.get("action")){
// Like button which runs in background
case "like":
//like article asynchronously
likeArticle(messagePayload.data.get("articleId"));
break;
// View button which runs in foreground
case "view":
//navigate to article page
//note, uiReference and uiReference.get() need to checked to ensure
// they are not null
goToArticle(uiReference, messagePayload.data.get("articleId"));
break;
}
}
}
Then add the setMessageResponseListener
option to your initialisation of the PushConnector:
new PushConnector.Builder(XPUSH_APP_KEY, GOOGLE_PROJECT_NUMBER)
.setMessageResponseListener(this)
...
.create(this);
Updated over 3 years ago