Commit d5c0072c by Sebastián Katzer

Support for Android 4.4 and Android 5+

parent 77ecc164
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
package de.appplant.cordova.plugin.notification; package de.appplant.cordova.plugin.notification;
import android.annotation.SuppressLint;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
...@@ -38,6 +39,7 @@ import java.util.Set; ...@@ -38,6 +39,7 @@ import java.util.Set;
import de.appplant.cordova.plugin.badge.BadgeImpl; import de.appplant.cordova.plugin.badge.BadgeImpl;
import static android.os.Build.VERSION.SDK_INT; 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.os.Build.VERSION_CODES.O;
import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_DEFAULT; import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_DEFAULT;
import static de.appplant.cordova.plugin.notification.Notification.PREF_KEY_ID; import static de.appplant.cordova.plugin.notification.Notification.PREF_KEY_ID;
...@@ -103,6 +105,7 @@ public final class Manager { ...@@ -103,6 +105,7 @@ public final class Manager {
/** /**
* TODO: temporary * TODO: temporary
*/ */
@SuppressLint("WrongConstant")
private void createDefaultChannel() { private void createDefaultChannel() {
NotificationManager mgr = getNotMgr(); NotificationManager mgr = getNotMgr();
...@@ -224,8 +227,8 @@ public final class Manager { ...@@ -224,8 +227,8 @@ public final class Manager {
if (type == Notification.Type.ALL) if (type == Notification.Type.ALL)
return getIds(); return getIds();
StatusBarNotification[] activeToasts = getNotMgr().getActiveNotifications(); StatusBarNotification[] activeToasts = getActiveNotifications();
List<Integer> activeIds = new ArrayList<Integer>(); List<Integer> activeIds = new ArrayList<Integer>();
for (StatusBarNotification toast : activeToasts) { for (StatusBarNotification toast : activeToasts) {
activeIds.add(toast.getId()); activeIds.add(toast.getId());
...@@ -390,6 +393,17 @@ public final class Manager { ...@@ -390,6 +393,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. * Shared private preferences for the application.
*/ */
private SharedPreferences getPrefs () { private SharedPreferences getPrefs () {
...@@ -411,11 +425,4 @@ public final class Manager { ...@@ -411,11 +425,4 @@ public final class Manager {
return NotificationManagerCompat.from(context); return NotificationManagerCompat.from(context);
} }
/**
* Notification manager compat for the application.
*/
private NotificationManagerCompat getNotMgrCompat () {
return NotificationManagerCompat.from(context);
}
} }
...@@ -46,6 +46,8 @@ import java.util.Set; ...@@ -46,6 +46,8 @@ import java.util.Set;
import static android.app.AlarmManager.RTC; import static android.app.AlarmManager.RTC;
import static android.app.AlarmManager.RTC_WAKEUP; import static android.app.AlarmManager.RTC_WAKEUP;
import static android.app.PendingIntent.FLAG_CANCEL_CURRENT; 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_MAX;
import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MIN; import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MIN;
...@@ -138,8 +140,9 @@ public final class Notification { ...@@ -138,8 +140,9 @@ public final class Notification {
* Notification type can be one of triggered or scheduled. * Notification type can be one of triggered or scheduled.
*/ */
public Type getType () { public Type getType () {
StatusBarNotification[] toasts = getNotMgr().getActiveNotifications(); Manager mgr = Manager.getInstance(context);
int id = getId(); StatusBarNotification[] toasts = mgr.getActiveNotifications();
int id = getId();
for (StatusBarNotification toast : toasts) { for (StatusBarNotification toast : toasts) {
if (toast.getId() == id) { if (toast.getId() == id) {
...@@ -202,7 +205,11 @@ public final class Notification { ...@@ -202,7 +205,11 @@ public final class Notification {
mgr.setExact(RTC, time, pi); mgr.setExact(RTC, time, pi);
break; break;
case IMPORTANCE_MAX: 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; break;
default: default:
mgr.setExact(RTC_WAKEUP, time, pi); mgr.setExact(RTC_WAKEUP, time, pi);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment