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

Commit

Permalink
🐛 Issue #1949: Guard notification channel on older OS versions
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed Oct 18, 2017
1 parent acbebfa commit a301e47
Showing 1 changed file with 52 additions and 44 deletions.
96 changes: 52 additions & 44 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,63 +58,71 @@ private Context getApplicationContext() {

@TargetApi(26)
private void deleteChannel(String channelId) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.deleteNotificationChannel(channelId);
// only call on Android O and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.deleteNotificationChannel(channelId);
}
}

@TargetApi(26)
private void createChannel(JSONObject channel) throws JSONException {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
// only call on Android O and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);

String packageName = getApplicationContext().getPackageName();
NotificationChannel mChannel = new NotificationChannel(channel.getString(CHANNEL_ID),
channel.optString(CHANNEL_DESCRIPTION, ""),
channel.optInt(CHANNEL_IMPORTANCE, NotificationManager.IMPORTANCE_DEFAULT));

int lightColor = channel.optInt(CHANNEL_LIGHT_COLOR, -1);
if (lightColor != -1) {
mChannel.setLightColor(lightColor);
}

String packageName = getApplicationContext().getPackageName();
NotificationChannel mChannel = new NotificationChannel(channel.getString(CHANNEL_ID),
channel.optString(CHANNEL_DESCRIPTION, ""),
channel.optInt(CHANNEL_IMPORTANCE, NotificationManager.IMPORTANCE_DEFAULT));
int visibility = channel.optInt(VISIBILITY, NotificationCompat.VISIBILITY_PUBLIC);
mChannel.setLockscreenVisibility(visibility);

boolean badge = channel.optBoolean(BADGE, true);
mChannel.setShowBadge(badge);

String sound = channel.optString(SOUND, "default");
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build();
if (SOUND_RINGTONE.equals(sound)) {
mChannel.setSound(android.provider.Settings.System.DEFAULT_RINGTONE_URI, audioAttributes);
} else if (sound != null && !sound.contentEquals(SOUND_DEFAULT)) {
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + packageName + "/raw/" + sound);
mChannel.setSound(soundUri, audioAttributes);
} else {
mChannel.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes);
}

int lightColor = channel.optInt(CHANNEL_LIGHT_COLOR, -1);
if (lightColor != -1) {
mChannel.setLightColor(lightColor);
}
//JSONArray pattern = channel.optJSONArray(CHANNEL_VIBRATION);
//mChannel.setVibrationPattern();

int visibility = channel.optInt(VISIBILITY, NotificationCompat.VISIBILITY_PUBLIC);
mChannel.setLockscreenVisibility(visibility);

boolean badge = channel.optBoolean(BADGE, true);
mChannel.setShowBadge(badge);

String sound = channel.optString(SOUND, "default");
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
.build();
if (SOUND_RINGTONE.equals(sound)) {
mChannel.setSound(android.provider.Settings.System.DEFAULT_RINGTONE_URI, audioAttributes);
} else if (sound != null && !sound.contentEquals(SOUND_DEFAULT)) {
Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + packageName + "/raw/" + sound);
mChannel.setSound(soundUri, audioAttributes);
} else {
mChannel.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes);
notificationManager.createNotificationChannel(mChannel);
}

//JSONArray pattern = channel.optJSONArray(CHANNEL_VIBRATION);
//mChannel.setVibrationPattern();

notificationManager.createNotificationChannel(mChannel);
}

@TargetApi(26)
private void notificationChannelExists(JSONObject options) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
List<NotificationChannel> channels = notificationManager.getNotificationChannels();
if (channels.size() == 0) {
NotificationChannel mChannel = new NotificationChannel(DEFAULT_CHANNEL_ID, "PhoneGap PushPlugin",
NotificationManager.IMPORTANCE_DEFAULT);
mChannel.enableVibration(options.optBoolean(VIBRATE, true));
notificationManager.createNotificationChannel(mChannel);
// only call on Android O and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
List<NotificationChannel> channels = notificationManager.getNotificationChannels();
if (channels.size() == 0) {
NotificationChannel mChannel = new NotificationChannel(DEFAULT_CHANNEL_ID, "PhoneGap PushPlugin",
NotificationManager.IMPORTANCE_DEFAULT);
mChannel.enableVibration(options.optBoolean(VIBRATE, true));
notificationManager.createNotificationChannel(mChannel);
}
}

}

@Override
Expand Down

0 comments on commit a301e47

Please sign in to comment.