Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Android 8 Support for Sound and Vibrate properties. #1696

Merged
merged 1 commit into from
Mar 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/android/notification/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public Notification build() {
.setTimeoutAfter(options.getTimeout())
.setLights(options.getLedColor(), options.getLedOn(), options.getLedOff());

if (sound != Uri.EMPTY && !isUpdate()) {
if (!sound.equals(Uri.EMPTY) && !isUpdate()) {
builder.setSound(sound);
}

Expand Down
75 changes: 61 additions & 14 deletions src/android/notification/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@

package de.appplant.cordova.plugin.notification;

import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.AudioAttributes;
import android.net.Uri;
import android.service.notification.StatusBarNotification;
import android.support.v4.app.NotificationManagerCompat;

Expand All @@ -44,6 +45,8 @@
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.O;
import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_DEFAULT;
import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_HIGH;
import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_LOW;
import static de.appplant.cordova.plugin.notification.Notification.PREF_KEY_ID;
import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERED;

Expand All @@ -53,13 +56,6 @@
* cancel or clear local notifications.
*/
public final class Manager {

// TODO: temporary
static final String CHANNEL_ID = "default-channel-id";

// TODO: temporary
private static final CharSequence CHANNEL_NAME = "Default channel";

// The application context
private Context context;

Expand All @@ -70,7 +66,6 @@ public final class Manager {
*/
private Manager(Context context) {
this.context = context;
createDefaultChannel();
}

/**
Expand Down Expand Up @@ -105,22 +100,74 @@ public Notification schedule (Request request, Class<?> receiver) {
}

/**
* TODO: temporary
* Build channel with options
* @param soundUri Uri for custom sound (empty to use default)
* @param shouldVibrate whether not vibration should occur during the notification
* @param hasSound whether or not sound should play during the notification
* @param channelName the name of the channel (null will pick an appropriate default name
* for the options provided).
* @return channel ID of newly created (or reused) channel
*/
public String buildChannelWithOptions(Uri soundUri, boolean shouldVibrate,
boolean hasSound, CharSequence channelName) {
String channelId;
CharSequence defaultChannelName;
int importance;

if (hasSound && shouldVibrate) {
channelId = Options.CHANNEL_ID_SOUND_VIBRATE;
defaultChannelName = Options.CHANNEL_NAME_SOUND_VIBRATE;
importance = IMPORTANCE_HIGH;
shouldVibrate = true;
} else if (hasSound) {
channelId = Options.CHANNEL_ID_SOUND;
defaultChannelName = Options.CHANNEL_NAME_SOUND;
importance = IMPORTANCE_DEFAULT;
shouldVibrate = false;
} else if (shouldVibrate) {
channelId = Options.CHANNEL_ID_VIBRATE;
defaultChannelName = Options.CHANNEL_NAME_VIBRATE;
importance = IMPORTANCE_LOW;
shouldVibrate = true;
} else {
channelId = Options.CHANNEL_ID_SILENT;
defaultChannelName = Options.CHANNEL_NAME_SILENT;
importance = IMPORTANCE_LOW;
shouldVibrate = false;
}

createChannel(channelId, channelName != null ? channelName : defaultChannelName,
importance, shouldVibrate, soundUri);

return channelId;
}

/**
* Create a channel
*/
@SuppressLint("WrongConstant")
private void createDefaultChannel() {
public void createChannel(String channelId, CharSequence channelName, int importance,
Boolean shouldVibrate, Uri soundUri) {
NotificationManager mgr = getNotMgr();

if (SDK_INT < O)
return;

NotificationChannel channel = mgr.getNotificationChannel(CHANNEL_ID);
NotificationChannel channel = mgr.getNotificationChannel(channelId);

if (channel != null)
return;

channel = new NotificationChannel(
CHANNEL_ID, CHANNEL_NAME, IMPORTANCE_DEFAULT);
channelId, channelName, importance);

channel.enableVibration(shouldVibrate);

if (!soundUri.equals(Uri.EMPTY)) {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setSound(soundUri, attributes);
}

mgr.createNotificationChannel(channel);
}
Expand Down
41 changes: 37 additions & 4 deletions src/android/notification/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import de.appplant.cordova.plugin.notification.action.ActionGroup;
import de.appplant.cordova.plugin.notification.util.AssetUtil;

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.O;
import static android.support.v4.app.NotificationCompat.DEFAULT_LIGHTS;
import static android.support.v4.app.NotificationCompat.DEFAULT_SOUND;
import static android.support.v4.app.NotificationCompat.DEFAULT_VIBRATE;
Expand All @@ -57,6 +59,24 @@
* methods to convert independent values into platform specific values.
*/
public final class Options {
// Default Channel ID for SDK < 26
static final String DEFAULT_CHANNEL_ID = "channel-id-default";

// Silent channel
static final String CHANNEL_ID_SILENT = "channel-id-silent";
static final CharSequence CHANNEL_NAME_SILENT = "Silent Notifications";

// Vibrate only channel
static final String CHANNEL_ID_VIBRATE = "vibrate-channel-id";
timkellypa marked this conversation as resolved.
Show resolved Hide resolved
static final CharSequence CHANNEL_NAME_VIBRATE = "Low Priority Notifications";

// Sound only channel
static final String CHANNEL_ID_SOUND = "sound-channel-id";
static final CharSequence CHANNEL_NAME_SOUND = "Medium Priority Notifications";

// Sound and vibrate channel
static final String CHANNEL_ID_SOUND_VIBRATE = "sound-vibrate-channel-id";
static final CharSequence CHANNEL_NAME_SOUND_VIBRATE = "High Priority Notifications";

// Key name for bundled sound extra
static final String EXTRA_SOUND = "NOTIFICATION_SOUND";
Expand Down Expand Up @@ -212,7 +232,20 @@ long getTimeout() {
* The channel id of that notification.
*/
String getChannel() {
return options.optString("channel", Manager.CHANNEL_ID);
// If channel is passed in or we have a low enough SDK for it not to matter, short-circuit.
if (!options.optString("channel").isEmpty() || SDK_INT < O) {
timkellypa marked this conversation as resolved.
Show resolved Hide resolved
return options.optString("channel", DEFAULT_CHANNEL_ID);
}

Uri soundUri = getSound();
boolean hasSound = !isWithoutSound();
boolean shouldVibrate = isWithVibration();
CharSequence channelName = options.optString("channelName", null);

String channelId = Manager.getInstance(context).buildChannelWithOptions(soundUri, shouldVibrate,
hasSound, channelName);

return channelId;
}

/**
Expand Down Expand Up @@ -398,22 +431,22 @@ int getSmallIcon() {
/**
* If the phone should vibrate.
*/
private boolean isWithVibration() {
public boolean isWithVibration() {
return options.optBoolean("vibrate", true);
}

/**
* If the phone should play no sound.
*/
private boolean isWithoutSound() {
public boolean isWithoutSound() {
Object value = options.opt("sound");
return value == null || value.equals(false);
}

/**
* If the phone should play the default sound.
*/
private boolean isWithDefaultSound() {
public boolean isWithDefaultSound() {
Object value = options.opt("sound");
return value != null && value.equals(true);
}
Expand Down