Skip to content

Commit

Permalink
feat(android): Allow to configure a default notification sound (#2682)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Apr 3, 2020
1 parent a6c91da commit 93eb9aa
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.getcapacitor.plugin.notification;

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

import com.getcapacitor.Config;
Expand All @@ -25,6 +27,7 @@ public class LocalNotification {
private static final String CONFIG_KEY_PREFIX = "plugins.LocalNotifications.";
private static final int RESOURCE_ID_ZERO_VALUE = 0;
private static int defaultSmallIconID = RESOURCE_ID_ZERO_VALUE;
private static int defaultSoundID = RESOURCE_ID_ZERO_VALUE;

private String title;
private String body;
Expand Down Expand Up @@ -66,8 +69,20 @@ public void setSchedule(LocalNotificationSchedule schedule) {
this.schedule = schedule;
}

public String getSound() {
return sound;
public String getSound(Context context) {
String soundPath = null;
int resId = RESOURCE_ID_ZERO_VALUE;
String name = getResourceBaseName(sound);
if (name != null) {
resId = getResourceID(context, name, "raw");
}
if (resId == RESOURCE_ID_ZERO_VALUE) {
resId = getDefaultSound(context);
}
if(resId != RESOURCE_ID_ZERO_VALUE){
soundPath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + resId;
}
return soundPath;
}

public void setSound(String sound) {
Expand Down Expand Up @@ -258,6 +273,29 @@ private static int getDefaultSmallIcon(Context context){
return resId;
}

private static int getDefaultSound(Context context){
if(defaultSoundID != RESOURCE_ID_ZERO_VALUE) return defaultSoundID;

int resId = RESOURCE_ID_ZERO_VALUE;
String soundConfigResourceName = Config.getString(CONFIG_KEY_PREFIX + "sound");
soundConfigResourceName = getResourceBaseName(soundConfigResourceName);

if(soundConfigResourceName != null){
resId = getResourceID(context, soundConfigResourceName, "raw");
}

defaultSoundID = resId;
return resId;
}

public static Uri getDefaultSoundUrl(Context context){
int soundId = LocalNotification.getDefaultSound(context);
if (soundId != RESOURCE_ID_ZERO_VALUE) {
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + soundId);
}
return null;
}

public boolean isScheduled() {
return this.schedule != null &&
(this.schedule.getOn() != null ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
Expand Down Expand Up @@ -107,6 +108,13 @@ public void createNotificationChannel() {
int importance = android.app.NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(DEFAULT_NOTIFICATION_CHANNEL_ID, name, importance);
channel.setDescription(description);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ALARM).build();
Uri soundUri = LocalNotification.getDefaultSoundUrl(context);
if (soundUri != null) {
channel.setSound(soundUri, audioAttributes);
}
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
android.app.NotificationManager notificationManager = context.getSystemService(android.app.NotificationManager.class);
Expand Down Expand Up @@ -153,23 +161,26 @@ private void buildNotification(NotificationManagerCompat notificationManager, Lo
.setAutoCancel(true)
.setOngoing(false)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setGroupSummary(localNotification.isGroupSummary())
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
.setGroupSummary(localNotification.isGroupSummary());


// support multiline text
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(localNotification.getBody()));

String sound = localNotification.getSound();
String sound = localNotification.getSound(context);
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.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
} else {
mBuilder.setDefaults(Notification.DEFAULT_ALL);
}


String group = localNotification.getGroup();
if (group != null) {
mBuilder.setGroup(group);
Expand Down
7 changes: 7 additions & 0 deletions core/src/core-plugin-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,13 @@ export interface LocalNotification {
body: string;
id: number;
schedule?: LocalNotificationSchedule;
/**
* Name of the audio file with extension.
* On iOS the file should be in the app bundle.
* On Android the file should be on res/raw folder.
* Doesn't work on Android version 26+ (Android O and newer), for
* Recommended format is .wav because is supported by both platforms.
*/
sound?: string;
/**
* Android-only: set a custom statusbar icon.
Expand Down
4 changes: 3 additions & 1 deletion site/docs-md/apis/local-notifications/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ The local notification plugin allows the following configuration values to be ad

- `smallIcon`: It allows you to set the default icon for the local notification.
- `iconColor`: It allows you to set the default color for the local notification icon.
- `sound`: It allows you to set the default notification sound. On Android 26+ it sets the default channel sound and can't be changed unless the app is uninstalled.

```json
"plugins": {
"LocalNotifications": {
"smallIcon": "ic_stat_icon_config_sample",
"iconColor": "#488AFF"
"iconColor": "#488AFF",
"sound": "beep.wav"
}
}
```
Expand Down

0 comments on commit 93eb9aa

Please sign in to comment.