소스 검색

Added clear and clearAll functions and onClear callback for android on Java-side and fixed Problem with manually cleared notifications on android.

PKnittel 11 년 전
부모
커밋
fd76e891a1
6개의 변경된 파일132개의 추가작업 그리고 10개의 파일을 삭제
  1. 0 2
      README.md
  2. 8 0
      plugin.xml
  3. 56 0
      src/android/DeleteIntentReceiver.java
  4. 58 5
      src/android/LocalNotification.java
  5. 1 1
      src/android/Options.java
  6. 9 2
      src/android/Receiver.java

+ 0 - 2
README.md

@@ -528,8 +528,6 @@ The launch mode for the main activity has to be set to `singleInstance`
 <activity ... android:launchMode="singleInstance" ... />
 ```
 
-### A notification cleared by the User is still shown as Triggered on Android
-It's not possible on android, to get informed about a User clearing an applications notifications. Currently the only way to prevent that sort of behavior is, to set the "ongoing" parameter to "true" and cancel the notification during the onclick event.
 
 ## Contributing
 

+ 8 - 0
plugin.xml

@@ -62,6 +62,12 @@
              * sound and it vibrates the phone.
             -->
             <receiver android:name="de.appplant.cordova.plugin.localnotification.Receiver" />
+			
+			<!--
+             * The delete intent receiver is triggered when the user clears a notification
+			 * manually. It unpersists the cleared notification from the shared preferences.
+            -->
+            <receiver android:name="de.appplant.cordova.plugin.localnotification.DeleteIntentReceiver" />
 
             <!--
              * This class is triggered upon reboot of the device. It needs to re-register
@@ -73,6 +79,7 @@
                     <action android:name="android.intent.action.BOOT_COMPLETED" />
                 </intent-filter>
             </receiver>
+			
 
             <!--
              * The receiver activity is triggered when a notification is clicked by a user.
@@ -93,6 +100,7 @@
         <source-file src="src/android/Options.java"           target-dir="src/de/appplant/cordova/plugin/localnotification" />
         <source-file src="src/android/Restore.java"           target-dir="src/de/appplant/cordova/plugin/localnotification" />
         <source-file src="src/android/ReceiverActivity.java"  target-dir="src/de/appplant/cordova/plugin/localnotification" />
+		<source-file src="src/android/DeleteIntentReceiver.java"  target-dir="src/de/appplant/cordova/plugin/localnotification" />
     </platform>
 
     <!-- wp8 -->

+ 56 - 0
src/android/DeleteIntentReceiver.java

@@ -0,0 +1,56 @@
+/*
+    Copyright 2013-2014 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.
+*/
+
+package de.appplant.cordova.plugin.localnotification;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+
+public class DeleteIntentReceiver extends BroadcastReceiver {
+
+    public static final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS";
+	
+	@Override
+	public void onReceive(Context context, Intent intent) {
+        Options options = null;
+        Bundle bundle   = intent.getExtras();
+        JSONObject args;
+
+        try {
+            args    = new JSONObject(bundle.getString(OPTIONS));
+            options = new Options(context).parse(args);
+        } catch (JSONException e) {
+            return;
+        }
+
+        // The context may got lost if the app was not running before
+        LocalNotification.setContext(context);
+        
+        LocalNotification.unpersist(options.getId());
+
+	}
+
+}

+ 58 - 5
src/android/LocalNotification.java

@@ -73,7 +73,7 @@ public class LocalNotification extends CordovaPlugin {
         if (action.equalsIgnoreCase("add")) {
             cordova.getThreadPool().execute( new Runnable() {
                 public void run() {               	
-                    JSONObject arguments = setInitDate(args).optJSONObject(0);
+                    JSONObject arguments = setInitDate(args.optJSONObject(0));
                     Options options      = new Options(context).parse(arguments);
 
                     add(options, true);
@@ -169,6 +169,7 @@ public class LocalNotification extends CordovaPlugin {
         
         persist(options.getId(), options.getJSONObject());
 
+        //Intent is called when the Notification gets fired
         Intent intent = new Intent(context, Receiver.class)
             .setAction("" + options.getId())
             .putExtra(Receiver.OPTIONS, options.getJSONObject().toString());
@@ -183,6 +184,58 @@ public class LocalNotification extends CordovaPlugin {
         am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
     }
 
+    /**
+     * Clear a specific notification without canceling repeating alarms
+     * 
+     * @param notificationID
+     *            The original ID of the notification that was used when it was
+     *            registered using add()
+     */
+    public static void clear (String notificationId){
+    	SharedPreferences settings = getSharedPreferences();
+    	Map<String, ?> alarms      = settings.getAll();
+        NotificationManager nc = getNotificationManager();
+
+        try {
+            nc.cancel(Integer.parseInt(notificationId));
+        } catch (Exception e) {}
+        
+        JSONObject arguments;
+		try {
+			arguments = new JSONObject(alarms.get(notificationId).toString());
+			Options options      = new Options(context).parse(arguments);
+			Date now = new Date();
+			if ((options.getInterval()!=0)){
+				persist(notificationId, setInitDate(arguments));
+			}
+			else if((new Date(options.getDate()).before(now))){
+				unpersist(notificationId);
+			}
+		} catch (JSONException e) {
+			e.printStackTrace();
+		}
+		
+        fireEvent("clear", notificationId, "");
+    }
+    
+    /**
+     * Clear all notifications without canceling repeating alarms
+     */
+    public static void clearAll (){
+        SharedPreferences settings = getSharedPreferences();
+        NotificationManager nc     = getNotificationManager();
+        Map<String, ?> alarms      = settings.getAll();
+        Set<String> alarmIds       = alarms.keySet();
+
+        for (String alarmId : alarmIds) {
+            clear(alarmId);
+        }
+
+        nc.cancelAll();
+    }
+
+    
+    
     /**
      * Cancel a specific notification that was previously registered.
      *
@@ -490,14 +543,14 @@ public class LocalNotification extends CordovaPlugin {
      * @param args The given JSONArray
      * @return A new JSONArray with the parameter "initialDate" set.
      */
-    private static JSONArray setInitDate(JSONArray args){
-    	long initialDate = args.optJSONObject(0).optLong("date", 0) * 1000;
+    private static JSONObject setInitDate(JSONObject arguments){
+    	long initialDate = arguments.optLong("date", 0) * 1000;
     	try {
-			args.optJSONObject(0).put("initialDate", initialDate);
+    		arguments.put("initialDate", initialDate);
 		} catch (JSONException e) {
 			e.printStackTrace();
 		}
-    	return args;
+    	return arguments;
     }
     
   

+ 1 - 1
src/android/Options.java

@@ -101,7 +101,7 @@ public class Options {
 
         return this;
     }
-
+    
     /**
      * Returns options as JSON object
      */

+ 9 - 2
src/android/Receiver.java

@@ -116,7 +116,13 @@ public class Receiver extends BroadcastReceiver {
     @SuppressLint("NewApi")
     private Builder buildNotification () {
         Uri sound = options.getSound();
-
+        
+        //DeleteIntent is called when the user clears a notification manually
+        Intent deleteIntent = new Intent(context, DeleteIntentReceiver.class)
+        	.setAction("" + options.getId())
+        	.putExtra(Receiver.OPTIONS, options.getJSONObject().toString());
+        PendingIntent dpi = PendingIntent.getBroadcast(context, 0, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
+        
         Builder notification = new NotificationCompat.Builder(context)
             .setDefaults(0) // Do not inherit any defaults
             .setContentTitle(options.getTitle())
@@ -127,7 +133,8 @@ public class Receiver extends BroadcastReceiver {
             .setLargeIcon(options.getIcon())
             .setAutoCancel(options.getAutoCancel())
             .setOngoing(options.getOngoing())
-            .setLights(options.getColor(), 500, 500);
+            .setLights(options.getColor(), 500, 500)
+            .setDeleteIntent(dpi);
 
         if (sound != null) {
             notification.setSound(sound);