Commit fd76e891 authored by PKnittel's avatar PKnittel
Browse files

Added clear and clearAll functions and onClear callback for android on...

Added clear and clearAll functions and onClear callback for android on Java-side and fixed Problem with manually cleared notifications on android.
parent bafe8b5e
Loading
Loading
Loading
Loading
+0 −2
Changes for README.md: 0 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -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
Changes for plugin.xml: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -63,6 +63,12 @@
            -->
            <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
             * the alarms with the AlarmManager since these alarms are lost in case of
@@ -74,6 +80,7 @@
                </intent-filter>
            </receiver>
			

            <!--
             * The receiver activity is triggered when a notification is clicked by a user.
             * The activity calls the background callback and brings the launch inten
@@ -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
Changes for src/android/DeleteIntentReceiver.java: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*
    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
Changes for src/android/LocalNotification.java: 58 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -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;
    }
    
  
+8 −1
Changes for src/android/Receiver.java: 8 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -117,6 +117,12 @@ public class Receiver extends BroadcastReceiver {
    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);
+1 −1

File changed.

Contains only whitespace changes.