Skip to content

Commit 02fbf17

Browse files
Merge pull request #58 from ickata/master
Ability to set custom notification sound
2 parents 49a11f3 + ae23056 commit 02fbf17

File tree

6 files changed

+30
-10
lines changed

6 files changed

+30
-10
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ You can pass several options to this function, everything is optional:
4848
|`ticker` |On Android you can show a different text in the statusbar, instead of the `body`. Default not set, so `body` is used.|
4949
|`at` |A JavaScript Date object indicating when the notification should be shown. Default 'now'.|
5050
|`badge` |On iOS (and some Android devices) you see a number on top of the app icon. On most Android devices you'll see this number in the notification center. Default not set (0).|
51-
|`sound` |Set this to `null` to suppress the default sound.|
51+
|`sound` |Notification sound. For custom notification sound, copy sound file in ```App_Resources/iOS``` (iOS) and ```App_Resources/Android/raw``` (Android). Set this to "default" (or do not set at all) in order to use default OS sound. Set this to `null` to suppress sound.|
5252
|`interval` |Set to one of `second minute hour day week month quarter year` if you want a recurring notification.|
5353
|`smallIcon` |On Android you can set a custom icon in the system tray. Pass in 'res://filename' (without the extension) which lives in App_Resouces/Android/drawable folders. If not passed, we look for a file named 'ic_stat_notify.png' in the App_Resources/Android/drawable folders. Default: the app icon.|
5454
|`largeIcon` |Same as `smallIcon`, but this one is shown when you expand the notification center. The optional file we look for is not 'ic_stat_notify.png' but 'ic_notify.png'.|
@@ -69,7 +69,7 @@ Note that after a reboot the `smallIcon` and `largeIcon` are not restored but fa
6969
ongoing: true, // makes the notification ongoing (Android only)
7070
smallIcon: 'res://heart',
7171
interval: 'minute',
72-
sound: null, // suppress the default sound
72+
sound: require("application").ios ? "customsound-ios.wav" : "customsound-android", // can be also `null`, "default"
7373
at: new Date(new Date().getTime() + (10 * 1000)) // 10 seconds from now
7474
}]).then(
7575
function() {
@@ -195,5 +195,4 @@ If the `requestPermission` or `schedule` functions previously ran you may want t
195195
Let us know what you need by opening a Github issue.
196196

197197
We're thinking about adding support for things like:
198-
- [Custom Notification sounds](https://github.com/EddyVerbruggen/nativescript-local-notifications/issues/3)
199198
- Interactive Notifications on iOS

local-notifications.android.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,14 @@ LocalNotifications.schedule = function (arg) {
6565

6666
options.atTime = options.at ? options.at.getTime() : new Date().getTime();
6767

68-
if (options.sound === undefined || options.sound === "default") {
69-
options.sound = android.media.RingtoneManager.getDefaultUri(android.media.RingtoneManager.TYPE_NOTIFICATION).toString();
68+
switch (options.sound) {
69+
case false:
70+
options.sound = null;
71+
break;
72+
case undefined:
73+
case "default":
74+
options.sound = android.media.RingtoneManager.getDefaultUri(android.media.RingtoneManager.TYPE_NOTIFICATION).toString();
75+
break;
7076
}
7177

7278
// TODO best move this to native lib so we can reuse it in the restorereceiver (dupe for now)
@@ -272,7 +278,7 @@ LocalNotifications.requestPermission = function (arg) {
272278
};
273279

274280
LocalNotifications._getSharedPreferences = function () {
275-
var PREF_KEY = com.telerik.localnotifications.NotificationRestoreReceiver.SHARED_PREFERENCES_KEY;
281+
var PREF_KEY = "LocalNotificationsPlugin"; // TODO: For some reason this is `null` and causes Java error...
276282
return context.getSharedPreferences(PREF_KEY, android.content.Context.MODE_PRIVATE);
277283
};
278284

local-notifications.ios.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,17 @@ LocalNotifications._schedulePendingNotifications = function () {
114114
userInfoDict.setObjectForKey(options.interval, "interval");
115115
notification.userInfo = userInfoDict;
116116

117-
if (options.sound === undefined || options.sound === "default") {
118-
notification.soundName = UILocalNotificationDefaultSoundName;
117+
switch (options.sound) {
118+
case null:
119+
case false:
120+
break;
121+
case undefined:
122+
case "default":
123+
notification.soundName = UILocalNotificationDefaultSoundName;
124+
break;
125+
default:
126+
notification.soundName = options.sound;
127+
break;
119128
}
120129

121130
if (options.interval !== 0) {

native-src/android/src/com/telerik/localnotifications/NotificationPublisher.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,20 @@
55
import android.content.BroadcastReceiver;
66
import android.content.Context;
77
import android.content.Intent;
8+
import android.net.Uri;
9+
import static android.R.attr.data;
810

911
public class NotificationPublisher extends BroadcastReceiver {
1012

1113
public static String NOTIFICATION_ID = "notification-id";
1214
public static String NOTIFICATION = "notification";
15+
public static String SOUND = "sound";
1316

1417
public void onReceive(Context context, Intent intent) {
1518
final Notification notification = intent.getParcelableExtra(NOTIFICATION);
19+
Uri d_sound = Uri.parse((String)("android.resource://" + context.getPackageName() + "/raw/" + notification.sound));
20+
notification.sound = d_sound;
1621
final int id = intent.getIntExtra(NOTIFICATION_ID, 0);
1722
((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(id, notification);
1823
}
19-
}
24+
}

native-src/android/src/com/telerik/localnotifications/NotificationRestoreReceiver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void onReceive(Context context, Intent intent) {
4949
.setContentText(options.optString("body"))
5050
.setSmallIcon(options.optInt("icon"))
5151
.setAutoCancel(true)
52-
.setSound(options.has("sound") ? Uri.parse(options.getString("sound")) : null)
52+
.setSound(options.has("sound") ? Uri.parse((String)("android.resource://" + context.getPackageName() + "/raw/" + options.optString("sound"))) : Uri.parse((String)("android.resource://" + context.getPackageName() + "/raw/notify")) )
5353
.setNumber(options.optInt("badge"))
5454
.setTicker(options.optString("ticker"));
5555

@@ -67,6 +67,7 @@ public void onReceive(Context context, Intent intent) {
6767
final Intent notificationIntent = new Intent(context, NotificationPublisher.class)
6868
.setAction(options.getString("id"))
6969
.putExtra(com.telerik.localnotifications.NotificationPublisher.NOTIFICATION_ID, options.optInt("id"))
70+
.putExtra(NotificationPublisher.SOUND, options.optString("sound"))
7071
.putExtra(com.telerik.localnotifications.NotificationPublisher.NOTIFICATION, notification);
7172

7273
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, options.optInt("id")+200, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
177 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)