Commit fd76e891 by PKnittel

Added clear and clearAll functions and onClear callback for android on Java-side…

Added clear and clearAll functions and onClear callback for android on Java-side and fixed Problem with manually cleared notifications on android.
parent bafe8b5e
...@@ -528,8 +528,6 @@ The launch mode for the main activity has to be set to `singleInstance` ...@@ -528,8 +528,6 @@ The launch mode for the main activity has to be set to `singleInstance`
<activity ... android:launchMode="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 ## Contributing
......
...@@ -64,6 +64,12 @@ ...@@ -64,6 +64,12 @@
<receiver android:name="de.appplant.cordova.plugin.localnotification.Receiver" /> <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 * 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 * the alarms with the AlarmManager since these alarms are lost in case of
* reboot. * reboot.
...@@ -74,6 +80,7 @@ ...@@ -74,6 +80,7 @@
</intent-filter> </intent-filter>
</receiver> </receiver>
<!-- <!--
* The receiver activity is triggered when a notification is clicked by a user. * The receiver activity is triggered when a notification is clicked by a user.
* The activity calls the background callback and brings the launch inten * The activity calls the background callback and brings the launch inten
...@@ -93,6 +100,7 @@ ...@@ -93,6 +100,7 @@
<source-file src="src/android/Options.java" target-dir="src/de/appplant/cordova/plugin/localnotification" /> <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/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/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> </platform>
<!-- wp8 --> <!-- wp8 -->
......
/*
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());
}
}
...@@ -73,7 +73,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -73,7 +73,7 @@ public class LocalNotification extends CordovaPlugin {
if (action.equalsIgnoreCase("add")) { if (action.equalsIgnoreCase("add")) {
cordova.getThreadPool().execute( new Runnable() { cordova.getThreadPool().execute( new Runnable() {
public void run() { public void run() {
JSONObject arguments = setInitDate(args).optJSONObject(0); JSONObject arguments = setInitDate(args.optJSONObject(0));
Options options = new Options(context).parse(arguments); Options options = new Options(context).parse(arguments);
add(options, true); add(options, true);
...@@ -169,6 +169,7 @@ public class LocalNotification extends CordovaPlugin { ...@@ -169,6 +169,7 @@ public class LocalNotification extends CordovaPlugin {
persist(options.getId(), options.getJSONObject()); persist(options.getId(), options.getJSONObject());
//Intent is called when the Notification gets fired
Intent intent = new Intent(context, Receiver.class) Intent intent = new Intent(context, Receiver.class)
.setAction("" + options.getId()) .setAction("" + options.getId())
.putExtra(Receiver.OPTIONS, options.getJSONObject().toString()); .putExtra(Receiver.OPTIONS, options.getJSONObject().toString());
...@@ -184,6 +185,58 @@ public class LocalNotification extends CordovaPlugin { ...@@ -184,6 +185,58 @@ public class LocalNotification extends CordovaPlugin {
} }
/** /**
* 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. * Cancel a specific notification that was previously registered.
* *
* @param notificationId * @param notificationId
...@@ -490,14 +543,14 @@ public class LocalNotification extends CordovaPlugin { ...@@ -490,14 +543,14 @@ public class LocalNotification extends CordovaPlugin {
* @param args The given JSONArray * @param args The given JSONArray
* @return A new JSONArray with the parameter "initialDate" set. * @return A new JSONArray with the parameter "initialDate" set.
*/ */
private static JSONArray setInitDate(JSONArray args){ private static JSONObject setInitDate(JSONObject arguments){
long initialDate = args.optJSONObject(0).optLong("date", 0) * 1000; long initialDate = arguments.optLong("date", 0) * 1000;
try { try {
args.optJSONObject(0).put("initialDate", initialDate); arguments.put("initialDate", initialDate);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
return args; return arguments;
} }
......
...@@ -117,6 +117,12 @@ public class Receiver extends BroadcastReceiver { ...@@ -117,6 +117,12 @@ public class Receiver extends BroadcastReceiver {
private Builder buildNotification () { private Builder buildNotification () {
Uri sound = options.getSound(); 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) Builder notification = new NotificationCompat.Builder(context)
.setDefaults(0) // Do not inherit any defaults .setDefaults(0) // Do not inherit any defaults
.setContentTitle(options.getTitle()) .setContentTitle(options.getTitle())
...@@ -127,7 +133,8 @@ public class Receiver extends BroadcastReceiver { ...@@ -127,7 +133,8 @@ public class Receiver extends BroadcastReceiver {
.setLargeIcon(options.getIcon()) .setLargeIcon(options.getIcon())
.setAutoCancel(options.getAutoCancel()) .setAutoCancel(options.getAutoCancel())
.setOngoing(options.getOngoing()) .setOngoing(options.getOngoing())
.setLights(options.getColor(), 500, 500); .setLights(options.getColor(), 500, 500)
.setDeleteIntent(dpi);
if (sound != null) { if (sound != null) {
notification.setSound(sound); notification.setSound(sound);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment