Commit b4631b8f by Sebastián Katzer

Restore notifications and alarms on device boot

parent eb41d94f
......@@ -120,17 +120,18 @@
android:launchMode="singleInstance"
android:theme="@android:style/Theme.Translucent"
android:exported="false" />
<!--
<receiver android:name="de.appplant.cordova.plugin.localnotification.RestoreReceiver" android:exported="false" >
<receiver
android:name="de.appplant.cordova.plugin.localnotification.RestoreReceiver"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" android:priority="500" />
</intent-filter>
</receiver>
-->
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest">
<!-- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</config-file>
......@@ -153,11 +154,11 @@
<source-file
src="src/android/ClearReceiver.java"
target-dir="src/de/appplant/cordova/plugin/localnotification" />
<!--
<source-file
src="src/android/RestoreReceiver.java"
target-dir="src/de/appplant/cordova/plugin/localnotification" />
-->
<source-file
src="src/android/notification/action/Action.java"
target-dir="src/de/appplant/cordova/plugin/notification/action" />
......@@ -173,11 +174,11 @@
<source-file
src="src/android/notification/receiver/AbstractClickReceiver.java"
target-dir="src/de/appplant/cordova/plugin/notification/receiver" />
<!--
<source-file
src="src/android/notification/receiver/AbstractRestoreReceiver.java"
target-dir="src/de/appplant/cordova/plugin/notification/receiver" />
-->
<source-file
src="src/android/notification/receiver/AbstractTriggerReceiver.java"
target-dir="src/de/appplant/cordova/plugin/notification/receiver" />
......
......@@ -23,9 +23,11 @@
package de.appplant.cordova.plugin.localnotification;
import de.appplant.cordova.plugin.notification.AbstractRestoreReceiver;
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.Request;
import de.appplant.cordova.plugin.notification.receiver.AbstractRestoreReceiver;
/**
* This class is triggered upon reboot of the device. It needs to re-register
......@@ -37,30 +39,34 @@ public class RestoreReceiver extends AbstractRestoreReceiver {
/**
* Called when a local notification need to be restored.
*
* @param notification
* Wrapper around the local notification
* @param request Set of notification options.
* @param toast Wrapper around the local notification.
*/
@Override
public void onRestore (Notification notification) {
if (notification.isScheduled()) {
notification.schedule();
public void onRestore (Request request, Notification toast) {
Manager mgr = Manager.getInstance(toast.getContext());
if (toast.isHighPrio()) {
toast.show();
} else {
notification.cancel();
toast.clear();
}
if (toast.isRepeating()) {
mgr.schedule(request, TriggerReceiver.class);
}
}
/**
* Build notification specified by options.
*
* @param builder
* Notification builder
* @param builder Notification builder.
*/
@Override
public Notification buildNotification (Builder builder) {
return builder
.setTriggerReceiver(TriggerReceiver.class)
.setClickActivity(ClickReceiver.class)
.setClearReceiver(ClearReceiver.class)
.setClickActivity(ClickActivity.class)
.build();
}
......
......@@ -136,7 +136,7 @@ public final class Builder {
.setOngoing(options.isSticky())
.setColor(options.getColor())
.setVisibility(options.getVisibility())
.setPriority(options.getPriority())
.setPriority(options.getPrio())
.setShowWhen(options.showClock())
.setUsesChronometer(options.showChronometer())
.setGroup(options.getGroup())
......@@ -315,10 +315,13 @@ public final class Builder {
return;
Intent intent = new Intent(context, clearReceiver)
.putExtras(extras)
.setAction(options.getIdentifier())
.putExtra(Notification.EXTRA_ID, options.getId());
if (extras != null) {
intent.putExtras(extras);
}
int reqCode = random.nextInt();
PendingIntent deleteIntent = PendingIntent.getBroadcast(
......@@ -339,12 +342,15 @@ public final class Builder {
return;
Intent intent = new Intent(context, clickActivity)
.putExtras(extras)
.putExtra(Notification.EXTRA_ID, options.getId())
.putExtra(Action.EXTRA_ID, Action.CLICK_ACTION_ID)
.putExtra(Options.EXTRA_LAUNCH, options.isLaunchingApp())
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
if (extras != null) {
intent.putExtras(extras);
}
int reqCode = random.nextInt();
PendingIntent contentIntent = PendingIntent.getActivity(
......@@ -386,12 +392,15 @@ public final class Builder {
*/
private PendingIntent getPendingIntentForAction (Action action) {
Intent intent = new Intent(context, clickActivity)
.putExtras(extras)
.putExtra(Notification.EXTRA_ID, options.getId())
.putExtra(Action.EXTRA_ID, action.getId())
.putExtra(Options.EXTRA_LAUNCH, action.isLaunchingApp())
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
if (extras != null) {
intent.putExtras(extras);
}
int reqCode = random.nextInt();
return PendingIntent.getActivity(
......
......@@ -49,6 +49,7 @@ import static android.app.AlarmManager.RTC_WAKEUP;
import static android.app.PendingIntent.FLAG_CANCEL_CURRENT;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.M;
import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH;
import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MAX;
import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MIN;
......@@ -115,32 +116,39 @@ public final class Notification {
/**
* Get application context.
*/
public Context getContext () {
public Context getContext() {
return context;
}
/**
* Get notification options.
*/
public Options getOptions () {
public Options getOptions() {
return options;
}
/**
* Get notification ID.
*/
public int getId () {
public int getId() {
return options.getId();
}
/**
* If it's a repeating notification.
*/
private boolean isRepeating () {
public boolean isRepeating() {
return getOptions().getTrigger().has("every");
}
/**
* If the notifications priority is high or above.
*/
public boolean isHighPrio() {
return getOptions().getPrio() >= PRIORITY_HIGH;
}
/**
* Notification type can be one of triggered or scheduled.
*/
public Type getType() {
......@@ -210,7 +218,7 @@ public final class Notification {
context, 0, intent, FLAG_CANCEL_CURRENT);
try {
switch (options.getPriority()) {
switch (options.getPrio()) {
case IMPORTANCE_MIN:
mgr.setExact(RTC, time, pi);
break;
......
......@@ -91,7 +91,7 @@ public final class Options {
* @param context The application context.
* @param options The options dict map.
*/
Options(Context context, JSONObject options) {
public Options(Context context, JSONObject options) {
this.context = context;
this.options = options;
this.assets = AssetUtil.getInstance(context);
......@@ -479,7 +479,7 @@ public final class Options {
/**
* Gets the notifications priority.
*/
int getPriority() {
int getPrio() {
int prio = options.optInt("priority");
return Math.min(Math.max(prio, PRIORITY_MIN), PRIORITY_MAX);
......
......@@ -21,7 +21,7 @@
* @APPPLANT_LICENSE_HEADER_END@
*/
package de.appplant.cordova.plugin.notification;
package de.appplant.cordova.plugin.notification.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
......@@ -31,6 +31,12 @@ import org.json.JSONObject;
import java.util.List;
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.Options;
import de.appplant.cordova.plugin.notification.Request;
/**
* 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
......@@ -41,42 +47,36 @@ abstract public class AbstractRestoreReceiver extends BroadcastReceiver {
/**
* Called on device reboot.
*
* @param context
* Application context
* @param intent
* Received intent with content data
* @param context Application context
* @param intent Received intent with content data
*/
@Override
public void onReceive (Context context, Intent intent) {
Manager notificationMgr =
Manager.getInstance(context);
List<JSONObject> options =
notificationMgr.getOptions();
for (JSONObject data : options) {
Builder builder = new Builder(context, data);
Manager mgr = Manager.getInstance(context);
List<JSONObject> toasts = mgr.getOptions();
Notification notification =
buildNotification(builder);
for (JSONObject data : toasts) {
Options options = new Options(context, data);
Request request = new Request(options);
Builder builder = new Builder(options);
Notification toast = buildNotification(builder);
onRestore(notification);
onRestore(request, toast);
}
}
/**
* Called when a local notification need to be restored.
*
* @param notification
* Wrapper around the local notification
* @param request Set of notification options.
* @param toast Wrapper around the local notification.
*/
abstract public void onRestore (Notification notification);
abstract public void onRestore (Request request, Notification toast);
/**
* Build notification specified by options.
*
* @param builder
* Notification builder
* @param builder Notification builder.
*/
abstract public Notification buildNotification (Builder builder);
......
......@@ -34,7 +34,7 @@ exports._defaults = {
color : null,
data : null,
defaults : 0,
foreground : false,
foreground : null,
group : null,
groupSummary : false,
icon : null,
......
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