Forráskód Böngészése

Added wakeup options for Android [See #627]

Sebastián Katzer 8 éve
szülő
commit
620d3fc49d

+ 1 - 1
README.md

@@ -98,7 +98,7 @@ A notification does have a set of configurable properties. Not all of them are s
 | id            | data          | actionGroupId | summary       | led           | showWhen      | channel       | actions       |
 | text          | icon          | attachments   | smallIcon     | color         | defaults      | launch        | groupSummary  |
 | title         | silent        | progressBar   | sticky        | vibrate       | priority      | mediaSession  | foreground    |
-| sound         | trigger       | group         | autoClear     | lockscreen    | number        | badge         |
+| sound         | trigger       | group         | autoClear     | lockscreen    | number        | badge         | wakeup        |
 
 For their default values see:
 

+ 1 - 0
plugin.xml

@@ -140,6 +140,7 @@
 
         <config-file target="AndroidManifest.xml" parent="/manifest">
             <!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> -->
+            <uses-permission android:name="android.permission.WAKE_LOCK" />
         </config-file>
 
         <source-file

+ 36 - 2
src/android/TriggerReceiver.java

@@ -21,13 +21,19 @@
 
 package de.appplant.cordova.plugin.localnotification;
 
+import android.content.Context;
 import android.os.Bundle;
+import android.os.PowerManager;
 
 import de.appplant.cordova.plugin.notification.Builder;
 import de.appplant.cordova.plugin.notification.Manager;
 import de.appplant.cordova.plugin.notification.Notification;
+import de.appplant.cordova.plugin.notification.Options;
 import de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver;
 
+import static android.content.Context.POWER_SERVICE;
+import static android.os.PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY;
+
 /**
  * The alarm receiver is triggered when a scheduled alarm is fired. This class
  * reads the information in the intent and displays this information in the
@@ -46,10 +52,16 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
     @Override
     public void onTrigger (Notification notification, Bundle bundle) {
         boolean isUpdate = bundle.getBoolean(Notification.EXTRA_UPDATE, false);
-        int badge        = notification.getOptions().getBadgeNumber();
+        Context context  = notification.getContext();
+        Options options  = notification.getOptions();
+        int badge        = options.getBadgeNumber();
 
         if (badge > 0) {
-            Manager.getInstance(notification.getContext()).setBadge(badge);
+            Manager.getInstance(context).setBadge(badge);
+        }
+
+        if (options.shallWakeUp()) {
+            wakeUp(context);
         }
 
         notification.show();
@@ -59,6 +71,28 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
         }
     }
 
+    /**
+     * Wakeup the device.
+     *
+     * @param context The application context.
+     */
+    private void wakeUp (Context context) {
+        PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
+
+        if (pm == null)
+            return;
+
+        int level =   PowerManager.SCREEN_DIM_WAKE_LOCK
+                    | PowerManager.ACQUIRE_CAUSES_WAKEUP;
+
+        PowerManager.WakeLock wakeLock = pm.newWakeLock(
+                level,"LocalNotification");
+
+        wakeLock.setReferenceCounted(false);
+        wakeLock.acquire(1000);
+        wakeLock.release(RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
+    }
+
     /**
      * Build notification specified by options.
      *

+ 8 - 1
src/android/notification/Options.java

@@ -192,6 +192,13 @@ public final class Options {
         return options.optBoolean("launch", true);
     }
 
+    /**
+     * wakeup flag for the notification.
+     */
+    public boolean shallWakeUp() {
+        return options.optBoolean("wakeup", true);
+    }
+
     /**
      * The channel id of that notification.
      */
@@ -335,7 +342,7 @@ public final class Options {
     /**
      * Icon resource ID for the local notification.
      */
-    public boolean hasLargeIcon () {
+    boolean hasLargeIcon() {
         String icon = options.optString("icon", null);
         return icon != null;
     }

+ 2 - 1
www/local-notification-util.js

@@ -54,7 +54,8 @@ exports._defaults = {
     text          : '',
     title         : '',
     trigger       : { type : 'calendar' },
-    vibrate       : false
+    vibrate       : false,
+    wakeup        : true
 };
 
 // Listener