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

Commit

Permalink
Issue #61: Android: add property to vibrate phone on received notific…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
macdonst committed Aug 12, 2015
1 parent 85191ee commit 198d841
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Parameter | Description
`options.android.senderID` | `String` Maps to the project number in the Google Developer Console.
`options.android.icon` | `String` Optional. The name of a drawable resource to use as the small-icon.
`options.android.iconColor` | `String` Optional. Sets the background color of the small icon. [Supported Formats](http://developer.android.com/reference/android/graphics/Color.html#parseColor(java.lang.String))
`options.android.sound` | `Boolean` Optional. If `true` it plays the sound specified in the push data or the default system sound. Default is `true`.
`options.android.vibrate` | `Boolean` Optional. If `true` the device vibrates on receipt of notification. Default is `true`.
`options.ios` | `JSON Object` iOS specific initialization options.
`options.windows` | `JSON Object` Windows specific initialization options.

Expand Down
47 changes: 27 additions & 20 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,8 @@ public void createNotification(Context context, Bundle extras) {
int requestCode = new Random().nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

int defaults = Notification.DEFAULT_ALL;

if (extras.getString("defaults") != null) {
try {
defaults = Integer.parseInt(extras.getString("defaults"));
} catch (NumberFormatException e) {}
}

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
//.setDefaults(defaults)
.setWhen(System.currentTimeMillis())
.setContentTitle(extras.getString("title"))
.setTicker(extras.getString("title"))
Expand All @@ -117,9 +108,20 @@ public void createNotification(Context context, Bundle extras) {
SharedPreferences prefs = context.getSharedPreferences("com.adobe.phonegap.push", Context.MODE_PRIVATE);
String localIcon = prefs.getString("icon", null);
String localIconColor = prefs.getString("iconColor", null);
boolean soundOption = prefs.getBoolean("sound", true);
boolean vibrateOption = prefs.getBoolean("vibrate", true);
Log.d(LOG_TAG, "stored icon=" + localIcon);
Log.d(LOG_TAG, "stored iconColor=" + localIconColor);
Log.d(LOG_TAG, "stored sound=" + soundOption);
Log.d(LOG_TAG, "stored vibrate=" + vibrateOption);

/*
* Notification Vibration
*/
if (vibrateOption) {
mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
}

/*
* Notification Icon Color
*
Expand Down Expand Up @@ -201,17 +203,22 @@ public void createNotification(Context context, Bundle extras) {
}
}

String soundname = extras.getString("soundname");
if (soundname == null) {
soundname = extras.getString("sound");
}
if (soundname != null) {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + context.getPackageName() + "/raw/" + soundname);
Log.d(LOG_TAG, sound.toString());
mBuilder.setSound(sound);
} else {
mBuilder.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI);
/*
* Notification Sound
*/
if (soundOption) {
String soundname = extras.getString("soundname");
if (soundname == null) {
soundname = extras.getString("sound");
}
if (soundname != null) {
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + context.getPackageName() + "/raw/" + soundname);
Log.d(LOG_TAG, sound.toString());
mBuilder.setSound(sound);
} else {
mBuilder.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI);
}
}

String message = extras.getString("message");
Expand Down
2 changes: 2 additions & 0 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public boolean execute(String action, JSONArray data, CallbackContext callbackCo
} catch (JSONException e) {
Log.d(LOG_TAG, "no iconColor option");
}
editor.putBoolean("sound", jo.optBoolean("sound", true));
editor.putBoolean("vibrate", jo.optBoolean("vibrate", true));
editor.commit();
}

Expand Down

0 comments on commit 198d841

Please sign in to comment.