Переглянути джерело

Basic support for channels for Android 8

Sebastián Katzer 8 роки тому
батько
коміт
519c452b74

+ 2 - 1
src/android/notification/Builder.java

@@ -95,8 +95,9 @@ public final class Builder {
             return new Notification(context, options);
         }
 
-        builder = new NotificationCompat.Builder(context, "channel")
+        builder = new NotificationCompat.Builder(context, Manager.CHANNEL_ID)
                 .setDefaults(options.getDefaults())
+                .setChannelId(options.getChannel())
                 .setContentTitle(options.getTitle())
                 .setContentText(options.getText())
                 .setTicker(options.getText())

+ 36 - 3
src/android/notification/Manager.java

@@ -22,13 +22,14 @@
 package de.appplant.cordova.plugin.notification;
 
 import android.app.AlarmManager;
-import android.app.NotificationChannelGroup;
+import android.app.NotificationChannel;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.graphics.Color;
 import android.os.Build;
 import android.support.v4.app.NotificationManagerCompat;
 
@@ -44,6 +45,10 @@ import java.util.Set;
 
 import de.appplant.cordova.plugin.notification.receiver.TriggerReceiver;
 
+import static android.os.Build.VERSION.SDK_INT;
+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;
 
 /**
@@ -53,7 +58,13 @@ import de.appplant.cordova.plugin.notification.receiver.TriggerReceiver;
  */
 public final class Manager {
 
-    // Context passed through constructor and used for notification builder.
+    // TODO: temporary
+    static final String CHANNEL_ID = "default-channel-id";
+
+    // TODO: temporary
+    private static final CharSequence CHANNEL_NAME = "Default channel";
+
+    // The application context
     private Context context;
 
     /**
@@ -61,8 +72,9 @@ public final class Manager {
      *
      * @param context Application context
      */
-    private Manager(Context context){
+    private Manager(Context context) {
         this.context = context;
+        createDefaultChannel();
     }
 
     /**
@@ -141,6 +153,27 @@ public final class Manager {
         return true;
     }
 
+    /**
+     * TODO: temporary
+     */
+    private void createDefaultChannel() {
+        NotificationManager mgr = (NotificationManager)
+                context.getSystemService(Context.NOTIFICATION_SERVICE);
+
+        if (SDK_INT < O)
+            return;
+
+        NotificationChannel channel = mgr.getNotificationChannel(CHANNEL_ID);
+
+        if (channel != null)
+            return;
+
+        channel = new NotificationChannel(
+                CHANNEL_ID, CHANNEL_NAME, IMPORTANCE_DEFAULT);
+
+        mgr.createNotificationChannel(channel);
+    }
+
     // /**
     //  * Clear local notification specified by ID.
     //  *

+ 7 - 0
src/android/notification/Options.java

@@ -172,6 +172,13 @@ public final class Options {
         return options.optString("group", null);
     }
 
+    /**
+     * The channel id of that notification.
+     */
+    String getChannel() {
+        return options.optString("channel", Manager.CHANNEL_ID);
+    }
+
     /**
      * If the group shall show a summary.
      */

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

@@ -66,6 +66,7 @@ exports.applyPlatformSpecificOptions = function () {
         defaults.showWhen     = true;
         defaults.defaults     = 0;
         defaults.priority     = 0;
+        defaults.channel      = undefined;
         break;
     }
 };