Przeglądaj źródła

Sound can be specified under Android.

Sebastián Katzer 12 lat temu
rodzic
commit
7d7dfe63d2

+ 26 - 0
README.md

@@ -30,6 +30,9 @@ cordova plugin rm de.appplant.cordova.plugin.local-notifications
 ```
 
 ## Release Notes
+#### Version 0.6.1 (not yet released)
+- [feature:] Sound can be specified under Android.
+
 #### Version 0.6.0 (16.11.2013)
 - Added WP8 support<br>
   *Based on the LiveTiles WP8 plugin made by* ***Jesse MacFadyen (purplecabbage)***
@@ -63,6 +66,7 @@ window.plugin.notification.local.add({
     title:      String, // the title of the message
     repeat:     String, // has the options of daily', 'weekly',''monthly','yearly')
     badge:      Number, // displays number badge to notification
+    sound:      String, // a sound to be played (iOS & Android)
     foreground: String, // a javascript function to be called if the app is running
     background: String, // a javascript function to be called if the app is in the background
 });
@@ -120,6 +124,27 @@ window.plugin.notification.local.add({ icon: 'ic_launcher' });
  */
 window.plugin.notification.local.add({ icon: 'ic_dialog_email' });
 ```
+
+### Notification sound under Android
+The default sound is `RingtoneManager.TYPE_NOTIFICATION`. But an specific sound can be dinfed through the `sound` property.<br>
+The sound must be a absolute or relative Uri pointing to the sound file.
+```javascript
+/**
+ * Plays the sound if the notification pop's up.
+ */
+window.plugin.notification.local.add({ sound: 'res/sounds/beep.mp3' });
+
+/**
+ * Plays the `RingtoneManager.TYPE_ALARM` sound.
+ */
+window.plugin.notification.local.add({ sound: 'TYPE_ALARM' });
+
+/**
+ * Plays no sound if the notification pop's up.
+ */
+window.plugin.notification.local.add({ sound: null });
+```
+
 ### Notification sound under iOS
 The sound must be located in your project's resources and must be a caf file.
 ```javascript
@@ -140,6 +165,7 @@ window.plugin.notification.local.add({ image: 'appdata:ApplicationIcon.png' })
 ```
 All images can be restored to the default ones by canceling the notification.
 
+
 ## Quirks
 ### Adding a notification under WP8
 An application can only display one notification at a time. Each time a new notification has to be added, the application live tile's data will be overwritten by the new ones.

+ 17 - 2
src/android/LocalNotificationOptions.java

@@ -15,6 +15,9 @@ import java.util.Date;
 import org.apache.cordova.CordovaInterface;
 import org.json.JSONObject;
 
+import android.media.RingtoneManager;
+import android.net.Uri;
+
 /**
  * Class that helps to store the options that can be specified per alarm.
  */
@@ -91,8 +94,20 @@ public class LocalNotificationOptions {
     /**
      * Gibt den Pfad zum Sound der Notification an.
      */
-    public String getSound () {
-        return options.optString("sound", null);
+    public Uri getSound () {
+        String sound = options.optString("sound", null);
+
+        if (sound != null) {
+            try {
+                int soundId = (Integer) RingtoneManager.class.getDeclaredField(sound).get(Integer.class);
+
+                return RingtoneManager.getDefaultUri(soundId);
+            } catch (Exception e) {
+                return Uri.parse(sound);
+            }
+        }
+
+        return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
     }
 
     /**

+ 2 - 11
src/android/LocalNotificationReceiver.java

@@ -103,17 +103,8 @@ public class LocalNotificationReceiver extends BroadcastReceiver {
 		.setContentText(options.getMessage())
 		.setNumber(options.getBadge())
 		.setTicker(options.getTitle())
-		.setSmallIcon(options.getIcon());
-
-		try {
-			if (isInBackground(context)) {
-				// app is in background
-				notification.setDefaults(Notification.DEFAULT_SOUND);
-			}
-		} catch (Exception e) {
-			// missing GET_TASKS permission
-			notification.setDefaults(Notification.DEFAULT_SOUND);
-		}
+		.setSmallIcon(options.getIcon())
+		.setSound(options.getSound());
 
 		setClickEvent(notification);
 

+ 2 - 1
www/local-notification.js

@@ -31,7 +31,8 @@ LocalNotification.prototype = {
 
         switch (device.platform) {
             case 'Android':
-                defaults.icon = 'icon'; break;
+                defaults.icon= 'icon';
+                defaults.sound = null; break;
             case 'iOS':
                 defaults.sound = ''; break;
             case 'WinCE': case 'Win32NT':