-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalNotification.java
More file actions
183 lines (155 loc) · 5.99 KB
/
LocalNotification.java
File metadata and controls
183 lines (155 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.phonegap.plugin.localnotification;
import java.util.Calendar;
import org.json.JSONArray;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CallbackContext;
/**
* This plugin utilizes the Android AlarmManager in combination with StatusBar
* notifications. When a local notification is scheduled the alarm manager takes
* care of firing the event. When the event is processed, a notification is put
* in the Android status bar.
*
* @author Daniel van 't Oever
*/
public class LocalNotification extends CordovaPlugin {
public static final String PLUGIN_NAME = "LocalNotification";
/**
* Delegate object that does the actual alarm registration. Is reused by the
* AlarmRestoreOnBoot class.
*/
private AlarmHelper alarm = null;
@Override
public boolean execute(String action, JSONArray optionsArr, CallbackContext callbackContext) {
alarm = new AlarmHelper(cordova.getActivity());
Log.d(PLUGIN_NAME, "Plugin execute called with action: " + action);
final AlarmOptions alarmOptions = new AlarmOptions();
alarmOptions.parseOptions(optionsArr);
/*
* Determine which action of the plugin needs to be invoked
*/
String alarmId = alarmOptions.getNotificationId();
if (action.equalsIgnoreCase("add")) {
final boolean daily = alarmOptions.isRepeatDaily();
final String title = alarmOptions.getAlarmTitle();
final String subTitle = alarmOptions.getAlarmSubTitle();
final String ticker = alarmOptions.getAlarmTicker();
persistAlarm(alarmId, optionsArr);
return this.add(daily, title, subTitle, ticker, alarmId, alarmOptions.getCal());
} else if (action.equalsIgnoreCase("cancel")) {
unpersistAlarm(alarmId);
this.cancelNotification(alarmId);
return false;
} else if (action.equalsIgnoreCase("cancelall")) {
this.cancelAllNotifications();
unpersistAlarmAll();
return false;
}
return true;
}
/**
* Set an alarm
*
* @param repeatDaily
* If true, the alarm interval will be set to one day.
* @param alarmTitle
* The title of the alarm as shown in the Android notification
* panel
* @param alarmSubTitle
* The subtitle of the alarm
* @param alarmId
* The unique ID of the notification
* @param cal
* A calendar object that represents the time at which the alarm
* should first be started
* @return A pluginresult.
*/
public boolean add(boolean repeatDaily, String alarmTitle, String alarmSubTitle, String alarmTicker,
String alarmId, Calendar cal) {
final long triggerTime = cal.getTimeInMillis();
final String recurring = repeatDaily ? "daily" : "onetime";
Log.d(PLUGIN_NAME, "Adding " + recurring + " notification: '" + alarmTitle + alarmSubTitle + "' with id: "
+ alarmId + " at timestamp: " + triggerTime);
boolean result = alarm.addAlarm(repeatDaily, alarmTitle, alarmSubTitle, alarmTicker, alarmId, cal);
if (result) {
return true;
} else {
return false;
}
}
/**
* Cancel a specific notification that was previously registered.
*
* @param notificationId
* The original ID of the notification that was used when it was
* registered using addNotification()
*/
public boolean cancelNotification(String notificationId) {
Log.d(PLUGIN_NAME, "cancelNotification: Canceling event with id: " + notificationId);
boolean result = alarm.cancelAlarm(notificationId);
if (result) {
return true;
} else {
return false;
}
}
/**
* Cancel all notifications that were created by this plugin.
*/
public boolean cancelAllNotifications() {
Log.d(PLUGIN_NAME, "cancelAllNotifications: cancelling all events for this application");
/*
* Android can only unregister a specific alarm. There is no such thing
* as cancelAll. Therefore we rely on the Shared Preferences which holds
* all our alarms to loop through these alarms and unregister them one
* by one.
*/
final SharedPreferences alarmSettings = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE);
final boolean result = alarm.cancelAll(alarmSettings);
return result;
}
/**
* Persist the information of this alarm to the Android Shared Preferences.
* This will allow the application to restore the alarm upon device reboot.
* Also this is used by the cancelAllNotifications method.
*
* @see #cancelAllNotifications()
*
* @param optionsArr
* The assumption is that parseOptions has been called already.
*
* @return true when successfull, otherwise false
*/
private boolean persistAlarm(String alarmId, JSONArray optionsArr) {
final Editor alarmSettingsEditor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
alarmSettingsEditor.putString(alarmId, optionsArr.toString());
return alarmSettingsEditor.commit();
}
/**
* Remove a specific alarm from the Android shared Preferences
*
* @param alarmId
* The Id of the notification that must be removed.
*
* @return true when successfull, otherwise false
*/
private boolean unpersistAlarm(String alarmId) {
final Editor alarmSettingsEditor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
alarmSettingsEditor.remove(alarmId);
return alarmSettingsEditor.commit();
}
/**
* Clear all alarms from the Android shared Preferences
*
* @return true when successfull, otherwise false
*/
private boolean unpersistAlarmAll() {
final Editor alarmSettingsEditor = cordova.getActivity().getSharedPreferences(PLUGIN_NAME, Context.MODE_PRIVATE).edit();
alarmSettingsEditor.clear();
return alarmSettingsEditor.commit();
}
}