Skip to content

Commit

Permalink
[expo-notifications] Add support for setting distinct values for "sou…
Browse files Browse the repository at this point in the history
…nd" and "vibrate"
  • Loading branch information
sjchmiela committed Feb 11, 2020
1 parent 27387a7 commit 212eeec
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ExpoNotificationBuilder implements NotificationBuilder {
private static final String BADGE_KEY = "badge";
private static final String BODY_KEY = "body";
private static final String PRIORITY_KEY = "priority";
private static final String VIBRATE_KEY = "vibrate";
private static final String THUMBNAIL_URI_KEY = "thumbnailUri";

private static final String EXTRAS_BADGE_KEY = "badge";
Expand Down Expand Up @@ -86,11 +87,19 @@ protected NotificationCompat.Builder createBuilder() {
if (shouldPlaySound()) {
// Attach default notification sound to the NotificationCompat.Builder
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
builder.setDefaults(NotificationCompat.DEFAULT_ALL); // sets default vibration too
} else {
// Remove any sound attached to the NotificationCompat.Builder
builder.setSound(null);
// Remove any sound attached by notification options.
}

if (shouldPlaySound() && shouldUseDefaultVibration()) {
builder.setDefaults(NotificationCompat.DEFAULT_ALL); // sets default vibration too
} else if (shouldUseDefaultVibration()) {
builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
} else if (shouldPlaySound()) {
builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
} else {
// Remove any sound or vibration attached by notification options.
builder.setDefaults(0);
// Remove any vibration pattern attached to the builder by overriding
// it with a no-vibrate pattern. It also doubles as a cue for the OS
Expand Down Expand Up @@ -157,6 +166,10 @@ private boolean shouldPlaySound() {
return mNotificationRequest.optBoolean(SOUND_KEY);
}

private boolean shouldUseDefaultVibration() {
return mNotificationRequest.optBoolean(SOUND_KEY) || mNotificationRequest.optBoolean(VIBRATE_KEY);
}

private boolean shouldSetBadge() {
return !mNotificationRequest.isNull(BADGE_KEY);
}
Expand Down

0 comments on commit 212eeec

Please sign in to comment.