Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Support Twilio Notify (#1306)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
viktormuller authored and macdonst committed Oct 26, 2016
1 parent d0547ba commit a9bb3bf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
35 changes: 35 additions & 0 deletions docs/PAYLOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> localeFormatData = new ArrayList<String>();
if (!localeObject.isNull(LOC_DATA)) {
String localeData = localeObject.getString(LOC_DATA);
Expand Down Expand Up @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit a9bb3bf

Please sign in to comment.