From a9bb3bf0a2ca57f68eafc39070fc125746bbbb23 Mon Sep 17 00:00:00 2001 From: viktormuller Date: Wed, 26 Oct 2016 02:36:15 +0100 Subject: [PATCH] Support Twilio Notify (#1306) * Add support for Twilio Notify GCM messages Allows developers to use Twilio Notify seamlessly with phonegap and hence allows cross platform development on both client and service side. * Describe Twilio Notify support in PAYLOAD.md * Improved Twilio Notify example --- docs/PAYLOAD.md | 35 +++++++++++++++++++ .../adobe/phonegap/push/GCMIntentService.java | 10 +++--- .../adobe/phonegap/push/PushConstants.java | 3 ++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/docs/PAYLOAD.md b/docs/PAYLOAD.md index 2a4077469..839ca7780 100644 --- a/docs/PAYLOAD.md +++ b/docs/PAYLOAD.md @@ -19,6 +19,7 @@ - [Huawei and Xiaomi Phones](#huawei-and-xiaomi-phones) - [Visibility](#visibility-of-notifications) - [Badges](#badges) + - [Support for Twilio Notify](#support-for-twilio-notify) - [iOS Behaviour](#ios-behaviour) - [Sound](#sound-1) - [Background Notifications](#background-notifications-1) @@ -1014,6 +1015,40 @@ service.send(message, { registrationTokens: [ deviceID ] }, function (err, respo }); ``` +## Support for Twilio Notify + +This plugin seamlessly supports payloads generated by Twilio Notify on Android. Specifically the parameters passed in to the Twilio REST API are available in the message payload passed to your app as follows: + +- `Title` --> `data.title` +- `Body` --> `data.message` +- `Sound` --> `data.sound` + +Here is an example request to Twilio REST API and the corresponding JSON received by your app. + +``` +curl 'https://notify.twilio.com/v1/Services/IS1e928b239609199df31d461071fd3d23/Notifications' -X POST \ +--data-urlencode 'Identity=Bob' \ +--data-urlencode 'Body=Hello Bob! Twilio Notify + Phonegap is awesome!' \ +--data-urlencode 'Title=Hello Bob!' \ +--data-urlencode 'Sound=chime' \ +-u [AccountSID]:[AuthToken] +``` + +The JSON received by your app will comply with the standards described in the sections above: + +```javascript +{ + "registration_ids": ["my device id"], + "data": { + "title": "Hello Bob!", + "message": "Hello Bob! Twilio Notify + Phonegap is awesome!", + "sound": "chime" + } +} +``` + +Note: "sound" and "soundname" are equivalent and are considered to be the same by the plugin. + # iOS Behaviour ## Sound diff --git a/src/android/com/adobe/phonegap/push/GCMIntentService.java b/src/android/com/adobe/phonegap/push/GCMIntentService.java index dcdc179da..eb0c67225 100644 --- a/src/android/com/adobe/phonegap/push/GCMIntentService.java +++ b/src/android/com/adobe/phonegap/push/GCMIntentService.java @@ -130,7 +130,7 @@ private String localizeKey(Context context, String key, String value) { JSONObject localeObject = new JSONObject(value); String localeKey = localeObject.getString(LOC_KEY); - + ArrayList localeFormatData = new ArrayList(); if (!localeObject.isNull(LOC_DATA)) { String localeData = localeObject.getString(LOC_DATA); @@ -168,11 +168,13 @@ private String localizeKey(Context context, String key, String value) { * Replace alternate keys with our canonical value */ private String normalizeKey(String key) { - if (key.equals(BODY) || key.equals(ALERT) || key.equals(GCM_NOTIFICATION_BODY)) { + if (key.equals(BODY) || key.equals(ALERT) || key.equals(GCM_NOTIFICATION_BODY) || key.equals(TWILIO_BODY)) { return MESSAGE; - } else if (key.equals(MSGCNT) || key.equals(BADGE)) { + } else if (key.equals(TWILIO_TITLE)) { + return TITLE; + }else if (key.equals(MSGCNT) || key.equals(BADGE)) { return COUNT; - } else if (key.equals(SOUNDNAME)) { + } else if (key.equals(SOUNDNAME) || key.equals(TWILIO_SOUND)) { return SOUND; } else if (key.startsWith(GCM_NOTIFICATION)) { return key.substring(GCM_NOTIFICATION.length()+1, key.length()); diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index 5726242a8..d9d6461b8 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -64,4 +64,7 @@ public interface PushConstants { public static final String INLINE_REPLY = "inlineReply"; public static final String LOC_KEY = "locKey"; public static final String LOC_DATA = "locData"; + public static final String TWILIO_BODY = "twi_body"; + public static final String TWILIO_TITLE = "twi_title"; + public static final String TWILIO_SOUND = "twi_sound"; }