Explorar el Código

Support for Android 4.4 and Android 5+

Sebastián Katzer hace 8 años
padre
commit
d5c0072cf3
Se han modificado 2 ficheros con 26 adiciones y 12 borrados
  1. 16 9
      src/android/notification/Manager.java
  2. 10 3
      src/android/notification/Notification.java

+ 16 - 9
src/android/notification/Manager.java

@@ -21,6 +21,7 @@
 
 package de.appplant.cordova.plugin.notification;
 
+import android.annotation.SuppressLint;
 import android.app.NotificationChannel;
 import android.app.NotificationManager;
 import android.content.Context;
@@ -38,6 +39,7 @@ import java.util.Set;
 import de.appplant.cordova.plugin.badge.BadgeImpl;
 
 import static android.os.Build.VERSION.SDK_INT;
+import static android.os.Build.VERSION_CODES.M;
 import static android.os.Build.VERSION_CODES.O;
 import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_DEFAULT;
 import static de.appplant.cordova.plugin.notification.Notification.PREF_KEY_ID;
@@ -103,6 +105,7 @@ public final class Manager {
     /**
      * TODO: temporary
      */
+    @SuppressLint("WrongConstant")
     private void createDefaultChannel() {
         NotificationManager mgr = getNotMgr();
 
@@ -224,8 +227,8 @@ public final class Manager {
         if (type == Notification.Type.ALL)
             return getIds();
 
-        StatusBarNotification[] activeToasts = getNotMgr().getActiveNotifications();
-        List<Integer> activeIds = new ArrayList<Integer>();
+        StatusBarNotification[] activeToasts = getActiveNotifications();
+        List<Integer> activeIds              = new ArrayList<Integer>();
 
         for (StatusBarNotification toast : activeToasts) {
             activeIds.add(toast.getId());
@@ -389,6 +392,17 @@ public final class Manager {
         }
     }
 
+    /**
+     * Get all active status bar notifications.
+     */
+    StatusBarNotification[] getActiveNotifications() {
+        if (SDK_INT >= M) {
+            return getNotMgr().getActiveNotifications();
+        } else {
+            return new StatusBarNotification[0];
+        }
+    }
+
     /**
      * Shared private preferences for the application.
      */
@@ -411,11 +425,4 @@ public final class Manager {
         return NotificationManagerCompat.from(context);
     }
 
-    /**
-     * Notification manager compat for the application.
-     */
-    private NotificationManagerCompat getNotMgrCompat () {
-        return NotificationManagerCompat.from(context);
-    }
-
 }

+ 10 - 3
src/android/notification/Notification.java

@@ -46,6 +46,8 @@ import java.util.Set;
 import static android.app.AlarmManager.RTC;
 import static android.app.AlarmManager.RTC_WAKEUP;
 import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
+import static android.os.Build.VERSION.SDK_INT;
+import static android.os.Build.VERSION_CODES.M;
 import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MAX;
 import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MIN;
 
@@ -138,8 +140,9 @@ public final class Notification {
      * Notification type can be one of triggered or scheduled.
      */
     public Type getType () {
-        StatusBarNotification[] toasts = getNotMgr().getActiveNotifications();
-        int id = getId();
+        Manager mgr                    = Manager.getInstance(context);
+        StatusBarNotification[] toasts = mgr.getActiveNotifications();
+        int id                         = getId();
 
         for (StatusBarNotification toast : toasts) {
             if (toast.getId() == id) {
@@ -202,7 +205,11 @@ public final class Notification {
                         mgr.setExact(RTC, time, pi);
                         break;
                     case IMPORTANCE_MAX:
-                        mgr.setExactAndAllowWhileIdle(RTC_WAKEUP, time, pi);
+                        if (SDK_INT >= M) {
+                            mgr.setExactAndAllowWhileIdle(RTC_WAKEUP, time, pi);
+                        } else {
+                            mgr.setExact(RTC, time, pi);
+                        }
                         break;
                     default:
                         mgr.setExact(RTC_WAKEUP, time, pi);