Ver código fonte

Add `autoCancel` flag to indicate that the notification shall be automatically canceled when the user clicks it

Sebastián Katzer 12 anos atrás
pai
commit
adf62bf2b4

+ 11 - 9
README.md

@@ -50,6 +50,7 @@ More informations can be found [here](https://build.phonegap.com/plugins/331).
 - [bugfix:] App throws an error on iOS if `message` is null.
 - [bugfix:] Removed extra line break on iOS if `title` is null or empty.
 - [bugfix:] Notification on iOS will be canceled if a new one with the same ID was added.
+- [enhancement:] Added `autoCancel` flag.
 
 #### Version 0.6.3 (12.12.2013)
 - [bugfix:] Black screen on Android.
@@ -99,15 +100,16 @@ All properties are optional. If no date object is given, the notification will p
 
 ```javascript
 window.plugin.notification.local.add({
-    id:         String, // a unique id of the notifiction
-    date:       Date,   // this expects a date object
-    message:    String, // the message that is displayed
-    title:      String, // the title of the message
-    repeat:     String, // has the options of daily', 'weekly',''monthly','yearly')
-    badge:      Number, // displays number badge to notification
-    sound:      String, // a sound to be played (iOS & Android)
-    foreground: String, // a javascript function to be called if the app is running
-    background: String, // a javascript function to be called if the app is in the background
+    id:         String,  // A unique id of the notifiction
+    date:       Date,    // This expects a date object
+    message:    String,  // The message that is displayed
+    title:      String,  // The title of the message
+    repeat:     String,  // Has the options of daily', 'weekly',''monthly','yearly')
+    badge:      Number,  // Displays number badge to notification
+    sound:      String,  // A sound to be played (iOS & Android)
+    autoCancel: Boolean, // Setting this flag and the notification is automatically canceled when the user clicks it
+    foreground: String,  // A javascript function to be called if the app is running
+    background: String,  // A javascript function to be called if the app is in the background
 });
 ```
 **Note:** On Android the notification id needs to be a string which can be converted to a number. If the ID has an invalid format, it will be ignored, but canceling the notification will fail.

+ 11 - 8
src/android/Options.java

@@ -38,10 +38,8 @@ import android.net.Uri;
 public class Options {
 
     private JSONObject options = new JSONObject();
-    private String id          = null;
     private String packageName = null;
     private long interval      = 0;
-    private long date          = 0;
 
     Options (Activity activity) {
         packageName = activity.getPackageName();
@@ -58,8 +56,6 @@ public class Options {
         String repeat = options.optString("repeat");
 
         this.options = options;
-        date         = options.optLong("date") * 1000;
-        id           = options.optString("id");
 
         if (repeat.equalsIgnoreCase("daily")) {
             interval = AlarmManager.INTERVAL_DAY;
@@ -85,7 +81,7 @@ public class Options {
      * Gibt die Zeit in Sekunden an, wann die Notification aufpoppen soll.
      */
     public long getDate() {
-        return date;
+        return options.optLong("date", 0) * 1000;
     }
 
     /**
@@ -103,14 +99,14 @@ public class Options {
      * Gibt die Nachricht der Notification an.
      */
     public String getMessage () {
-        return options.optString("message");
+        return options.optString("message", "");
     }
 
     /**
      * Gibt den Titel der Notification an.
      */
     public String getTitle () {
-        return options.optString("title");
+        return options.optString("title", "");
     }
 
     /**
@@ -184,7 +180,14 @@ public class Options {
      * Gibt die Callback-ID des PluginResults an.
      */
     public String getId () {
-        return id;
+        return options.optString("id", "0");
+    }
+
+    /**
+     * Gibt an, ob die Notification automatisch geschlossen werden soll, wenn der Benutzer darauf klickt.
+     */
+    public Boolean getAutoCancel () {
+        return options.optBoolean("autoCancel", false);
     }
 
     /**

+ 2 - 1
src/android/Receiver.java

@@ -121,7 +121,8 @@ public class Receiver extends BroadcastReceiver {
         .setNumber(options.getBadge())
         .setTicker(options.getTitle())
         .setSmallIcon(options.getIcon())
-        .setSound(options.getSound());
+        .setSound(options.getSound())
+        .setAutoCancel(options.getAutoCancel());
 
         setClickEvent(notification);
 

+ 12 - 5
src/ios/APPLocalNotification.m

@@ -23,8 +23,6 @@
 
 @interface APPLocalNotification (Private)
 
-// Entfernt die zur ID passende Meldung
- - (void) cancelNotificationWithId:(NSString*)notificationId;
 - (NSMutableDictionary*) repeatDict;
 // Alle zusätzlichen Metadaten der Notification als Hash
 - (NSDictionary*) userDict:(NSMutableDictionary*)options;
@@ -58,7 +56,9 @@
 }
 
 /**
- * Entfernt die zur ID passende Meldung.
+ * Entfernt den anhand der ID angegebenen Eintrag.
+ *
+ * @param {NSString} id Die ID der Notification
  */
 - (void) cancel:(CDVInvokedUrlCommand*)command
 {
@@ -79,7 +79,7 @@
 }
 
 /**
- * Entfernt die zur ID passende Meldung.
+ * Entfernt den anhand der ID angegebenen Eintrag.
  *
  * @param {NSString} id Die ID der Notification
  */
@@ -127,8 +127,9 @@
     NSString* id = [options objectForKey:@"id"];
     NSString* bg = [options objectForKey:@"background"];
     NSString* fg = [options objectForKey:@"foreground"];
+    NSString* ac = [options objectForKey:@"autoCancel"];
 
-    return [NSDictionary dictionaryWithObjectsAndKeys:id, @"id", bg, @"background", fg, @"foreground", nil];
+    return [NSDictionary dictionaryWithObjectsAndKeys:id, @"id", bg, @"background", fg, @"foreground", ac, @"autoCancel", nil];
 }
 
 /**
@@ -191,6 +192,12 @@
     NSString* id                      = [notification.userInfo objectForKey:@"id"];
     NSString* callbackType            = isActive ? @"foreground" : @"background";
     NSString* callbackFn              = [notification.userInfo objectForKey:callbackType];
+    BOOL autoCancel                   = [[notification.userInfo objectForKey:@"autoCancel"] boolValue];
+
+    if (autoCancel && !isActive)
+    {
+        [[UIApplication sharedApplication] cancelLocalNotification:notification];
+    }
 
     if (callbackFn && callbackFn.length > 0)
     {

+ 6 - 0
src/wp8/LocalNotificationOptions.cs

@@ -92,6 +92,12 @@ namespace De.APPPlant.Cordova.Plugin.LocalNotification
         [DataMember(IsRequired = false, Name = "foreground")]
         public string Foreground { get; set; }
 
+        /// <summary>
+        /// Setting this flag will make it so the notification is automatically canceled when the user clicks it
+        /// </summary>
+        [DataMember(IsRequired = false, Name = "autoCancel")]
+        public bool AutoCancel { get; set; }
+
         /// <summary>
         /// The notification small background image to be displayed
         /// </summary>

+ 23 - 10
www/local-notification.js

@@ -1,11 +1,23 @@
-/**
- *  locale-notification.js
- *  Cordova LocalNotification Plugin
- *
- *  Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
- *  Copyright 2013 Sebastian Katzer. All rights reserved.
- *  GPL v2 licensed
- */
+/*
+    Copyright 2013 appPlant UG
+
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    "License"); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+*/
 
 var LocalNotification = function () {
 
@@ -23,11 +35,12 @@ LocalNotification.prototype = {
             date:       new Date(),
             message:    '',
             title:      '',
+            autoCancel: false,
             badge:      0,
             id:         0,
             repeat:     '',
-            background: undefined,
-            foreground: undefined
+            background: '',
+            foreground: ''
         };
 
         switch (device.platform) {