Browse Source

Support for native badges on Android via badge plugin

Sebastián Katzer 8 năm trước cách đây
mục cha
commit
be881a56d9

+ 3 - 1
plugin.xml

@@ -43,12 +43,13 @@
         <engine name="cordova"         version=">=3.6.0"  />
         <engine name="cordova-plugman" version=">=4.3.0"  />
         <engine name="cordova-windows" version=">=4.2.0"  />
-        <engine name="apple-ios"       version=">=10.0.0" />
         <engine name="cordova-android" version=">=6.0.0"  />
+        <engine name="apple-ios"       version=">=10.0.0" />
     </engines>
 
     <!-- dependencies -->
     <dependency id="cordova-plugin-device" />
+    <dependency id="cordova-plugin-badge" version=">=0.8.5" />
 
     <!-- js -->
     <js-module src="www/local-notification.js" name="LocalNotification">
@@ -96,6 +97,7 @@
     <!-- android -->
     <platform name="android">
         <framework src="com.android.support:support-v4:26.+" value="gradle" />
+        <framework src="src/android/build/localnotification.gradle" custom="true" type="gradleReference"/>
 
         <config-file target="res/xml/config.xml" parent="/*">
             <feature name="LocalNotification">

+ 7 - 0
src/android/TriggerReceiver.java

@@ -24,6 +24,7 @@ package de.appplant.cordova.plugin.localnotification;
 import android.os.Bundle;
 
 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.receiver.AbstractTriggerReceiver;
 
@@ -44,6 +45,12 @@ public class TriggerReceiver extends AbstractTriggerReceiver {
      */
     @Override
     public void onTrigger (Notification notification, Bundle bundle) {
+        int badge = notification.getOptions().getBadgeNumber();
+
+        if (badge > 0) {
+            Manager.getInstance(notification.getContext()).setBadge(badge);
+        }
+
         notification.show();
         LocalNotification.fireEvent("trigger", notification);
     }

+ 28 - 0
src/android/build/localnotification.gradle

@@ -0,0 +1,28 @@
+/*
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apache License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. Please obtain a copy of the License at
+ * http://opensource.org/licenses/Apache-2.0/ and read it before using this
+ * file.
+ *
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
+ */
+
+repositories {
+    mavenCentral()
+}
+
+if (!project.ext.has('appShortcutBadgerVersion')) {
+    ext.appShortcutBadgerVersion = '1.1.19'
+}
+
+dependencies {
+    compile "me.leolin:ShortcutBadger:${appShortcutBadgerVersion}@aar"
+}

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

@@ -123,7 +123,7 @@ public final class Builder {
                 .setContentTitle(options.getTitle())
                 .setContentText(options.getText())
                 .setTicker(options.getText())
-                .setNumber(options.getBadgeNumber())
+                .setNumber(options.getNumber())
                 .setAutoCancel(options.isAutoClear())
                 .setOngoing(options.isSticky())
                 .setColor(options.getColor())

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

@@ -30,6 +30,8 @@ import android.support.v4.app.NotificationManagerCompat;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import de.appplant.cordova.plugin.badge.BadgeImpl;
+
 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;
@@ -160,6 +162,7 @@ public final class Manager {
      */
     public void clearAll () {
         getNotCompMgr().cancelAll();
+        setBadge(0);
     }
 
     // /**
@@ -461,6 +464,19 @@ public final class Manager {
     //     return obj1;
     // }
 
+    /**
+     * Set the badge number of the app icon.
+     *
+     * @param badge The badge number.
+     */
+    public void setBadge (int badge) {
+        if (badge == 0) {
+            new BadgeImpl(context).clearBadge();
+        } else {
+            new BadgeImpl(context).setBadge(badge);
+        }
+    }
+
     /**
      * Shared private preferences for the application.
      */

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

@@ -138,10 +138,17 @@ public final class Options {
     /**
      * Badge number for the local notification.
      */
-    int getBadgeNumber() {
+    public int getBadgeNumber() {
         return options.optInt("badge", 0);
     }
 
+    /**
+     * Number for the local notification.
+     */
+    public int getNumber() {
+        return options.optInt("number", 0);
+    }
+
     /**
      * ongoing flag for local notifications.
      */

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

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