Skip to content

Commit

Permalink
Added channel creation utility and notification sound support
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanadero committed Oct 8, 2019
1 parent fc77247 commit 9ce6034
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public static Builder from(Context context, JSONObject jsonNotification) {

// Schedule
try {
builder.setSchedule(RichLocalNotificationSchedule.buildFromJson(notification.getJSObject("schedule")));
JSObject schedule = notification.getJSObject(SCHEDULE);
builder.setSchedule(RichLocalNotificationSchedule.buildFromJson(schedule));
} catch (ParseException e) {
Log.e(LogUtils.getPluginTag("RLN"), "Invalid notification schedule date");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.okode.richlocalnotifications;

import android.app.NotificationChannel;
import android.content.Context;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;

@RequiresApi(api = Build.VERSION_CODES.O)
public class RichLocalNotificationChannelBuilder {

private String id;
private String name;
private String description;
private int importance;
private boolean showBadge;
private boolean enableVibration;
private int lockScreenVisibility;
private String sound;

public RichLocalNotificationChannelBuilder(String id, String name, int importance) {
this.setId(id);
this.setName(name);
this.setImportance(importance);
}

public RichLocalNotificationChannelBuilder setId(String id) {
this.id = id;
return this;
}

public RichLocalNotificationChannelBuilder setName(String name) {
this.name = name;
return this;
}

public RichLocalNotificationChannelBuilder setDescription(String description) {
this.description = description;
return this;
}

public RichLocalNotificationChannelBuilder setImportance(int importance) {
this.importance = importance;
return this;
}

public RichLocalNotificationChannelBuilder setShowBadge(boolean showBadge) {
this.showBadge = showBadge;
return this;
}

public RichLocalNotificationChannelBuilder setEnableVibration(boolean enableVibration) {
this.enableVibration = enableVibration;
return this;
}

public RichLocalNotificationChannelBuilder setLockScreenVisibility(int lockScreenVisibility) {
this.lockScreenVisibility = lockScreenVisibility;
return this;
}

public RichLocalNotificationChannelBuilder setSound(String sound) {
this.sound = sound;
return this;
}

public NotificationChannel build(Context context) {
NotificationChannel channel = new NotificationChannel(this.id, this.name, this.importance);

channel.setDescription(this.description);
channel.setShowBadge(this.showBadge);
channel.enableVibration(this.enableVibration);
channel.setLockscreenVisibility(this.lockScreenVisibility);

// Audio configuration
Uri soundUri = RichLocalNotificationUtils.getSound(context, this.sound);

AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build();

channel.setSound(soundUri, audioAttributes);

return channel;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
Expand Down Expand Up @@ -123,33 +122,28 @@ protected String getDefaultChannelName() {
protected NotificationCompat.Builder getNotificationBuilder(RichLocalNotification richLocalNotification) {
String channelId = richLocalNotification.getChannelId() != null ?
richLocalNotification.getChannelId() : getDefaultChannelId();

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setContentTitle(richLocalNotification.getTitle())
.setContentText(richLocalNotification.getBody())
.setAutoCancel(true)
.setOngoing(false)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOnlyAlertOnce(true)
.setSmallIcon(richLocalNotification.getSmallIconId())
.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);

// Set priority
if (richLocalNotification.getPriority() != null) {
mBuilder.setPriority(richLocalNotification.getPriority());
} else {
mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
}

String sound = richLocalNotification.getSound();
if (sound != null) {
Uri soundUri = Uri.parse(sound);
// Grant permission to use sound
context.grantUriPermission(
"com.android.systemui", soundUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
mBuilder.setSound(soundUri);
}

mBuilder.setVisibility(Notification.VISIBILITY_PRIVATE);
mBuilder.setOnlyAlertOnce(true);
// Set sound
Uri sound = RichLocalNotificationUtils.getSound(context, richLocalNotification.getSound());
mBuilder.setSound(sound);

mBuilder.setSmallIcon(richLocalNotification.getSmallIconId());
return mBuilder;
}

Expand Down Expand Up @@ -178,16 +172,13 @@ protected Intent buildIntent(RichLocalNotification richLocalNotification, String

private void createHighPriorityNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
NotificationChannel channel = new RichLocalNotificationChannelBuilder(
getDefaultChannelId(),
getDefaultChannelName(), NotificationManager.IMPORTANCE_HIGH);
channel.setShowBadge(true);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build();
channel.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI, audioAttributes);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
channel.enableVibration(true);
getDefaultChannelName(), NotificationManager.IMPORTANCE_HIGH)
.setShowBadge(true)
.setEnableVibration(true)
.setLockScreenVisibility(Notification.VISIBILITY_PUBLIC)
.build(this.context);
android.app.NotificationManager notificationManager =
context.getSystemService(android.app.NotificationManager.class);
notificationManager.createNotificationChannel(channel);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.okode.richlocalnotifications;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;

public class RichLocalNotificationUtils {

public static final String SOUND_SYSTEM_RINGTONE = "system_ringtone";

private RichLocalNotificationUtils() { }

public static Uri getSound(Context context, String sound) {
Uri soundUri = android.provider.Settings.System.DEFAULT_NOTIFICATION_URI;

if (SOUND_SYSTEM_RINGTONE.equalsIgnoreCase(sound)) {
soundUri = android.provider.Settings.System.DEFAULT_RINGTONE_URI;
} else if (sound != null && !sound.isEmpty()) {
soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/raw/" + sound);
}

return soundUri;
}
}

0 comments on commit 9ce6034

Please sign in to comment.