Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,18 @@ Cancels the notification the given notification id.
id: The notification id.
```

### `cancelwithcallback(int id, Function success, Function failure)`

### `cancelAll()`
Cancels the notification the given notification id. `success` and `failure` refer to function callbacks. `failure` will trigger if the cancellation fails, `success` will trigger if the cancellation succeeds.

Cancels all notifications.
```
id: The notification id.
```


### `cancelAll(Function success, Function failure)`

Cancels all notifications. `success` and `failure` refer to function callbacks. `failure` will trigger if any one of the cancellation operations, which may be a partial success. `success` will only trigger with complete success.


### `setApplicationBadge(int value)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public void onCreate(Bundle savedInstanceState) {
int notificationId = 0; // Default Id
try {
notificationId = bundle.getInt(AlarmReceiver.NOTIFICATION_ID);
Log.d("AlarmHelper", "Opening Activity with: " + notificationId);
Log.d(LocalNotification.TAG, "Opening Activity with: " + notificationId);
} catch (Exception e) {
try {
notificationId = Integer.parseInt(bundle.getString(AlarmReceiver.NOTIFICATION_ID));
Log.d("AlarmHelper", "Opening Activity with: " + notificationId);
Log.d(LocalNotification.TAG, "Opening Activity with: " + notificationId);
} catch (Exception e2) {
}
}
Expand Down Expand Up @@ -87,6 +87,7 @@ public boolean cancelAlarm(String notificationId) {
try {
getAlarmManager().cancel(pi);
} catch (Exception e) {
Log.e(LocalNotification.TAG, "Exception: " + e);
return false;
}
return true;
Expand All @@ -97,6 +98,8 @@ public boolean cancelAlarm(String notificationId) {
*/
public boolean cancelAll(SharedPreferences alarmSettings) {
Set<String> alarmIds = alarmSettings.getAll().keySet();

Log.d(LocalNotification.TAG, "Number of alarmIds: " + alarmIds.size());

for (String alarmId : alarmIds) {
Log.d(LocalNotification.TAG, "Canceling notification with id: " + alarmId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

/**
* Class that helps to store the options that can be specified per alarm.
*
Expand Down Expand Up @@ -41,23 +43,10 @@ public AlarmOptions(JSONArray options, Context context) {
* JSON Array containing the list options.
*/
public void parseOptions(JSONArray optionsArr, Context context) {
final JSONObject options = optionsArr.optJSONObject(0);
final JSONObject options = optionsArr.optJSONObject(1);

if (options != null) {

// Parse string representing the date
String textDate = options.optString("date");
if (!"".equals(textDate)) {
String[] datePart = textDate.split("/");
int month = Integer.parseInt(datePart[0]);
int day = Integer.parseInt(datePart[1]);
int year = Integer.parseInt(datePart[2]);
int hour = Integer.parseInt(datePart[3]);
int min = Integer.parseInt(datePart[4]);

cal.set(year, month, day, hour, min);
}

String optString = options.optString("message");
if (!"".equals(optString)) {
String lines[] = optString.split("\\r?\\n");
Expand All @@ -73,62 +62,63 @@ public void parseOptions(JSONArray optionsArr, Context context) {
this.icon = android.R.drawable.btn_star_big_on;
}

this.alarmTicker = options.optString("ticker");
this.alarmTicker = options.optString("ticker");
this.repeatDaily = options.optBoolean("repeatDaily");
this.notificationId = options.optString("id");
}
}

public Calendar getCal() {
return cal;
return cal;
}

public void setCal(Calendar cal) {
this.cal = cal;
this.cal = cal;
}

public String getAlarmTitle() {
return alarmTitle;
return alarmTitle;
}

public void setAlarmTitle(String alarmTitle) {
this.alarmTitle = alarmTitle;
this.alarmTitle = alarmTitle;
}

public String getAlarmSubTitle() {
return alarmSubTitle;
return alarmSubTitle;
}

public void setAlarmSubTitle(String alarmSubTitle) {
this.alarmSubTitle = alarmSubTitle;
this.alarmSubTitle = alarmSubTitle;
}

public String getAlarmTicker() {
return alarmTicker;
return alarmTicker;
}

public void setAlarmTicker(String alarmTicker) {
this.alarmTicker = alarmTicker;
this.alarmTicker = alarmTicker;
}

public boolean isRepeatDaily() {
return repeatDaily;
return repeatDaily;
}

public int getIcon() { return this.icon; }

public void setIcon(int icon) { this.icon = icon; }

public void setRepeatDaily(boolean repeatDaily) {
this.repeatDaily = repeatDaily;
this.repeatDaily = repeatDaily;
}

public String getNotificationId() {
return notificationId;
return notificationId;
}

public void setNotificationId(String notificationId) {
this.notificationId = notificationId;
this.notificationId = notificationId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import android.os.Bundle;
import android.util.Log;

import android.content.SharedPreferences;

/**
* The alarm receiver is triggered when a scheduled alarm is fired. This class
* reads the information in the intent and displays this information in the
Expand All @@ -31,7 +33,7 @@ public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
Log.d("AlarmReceiver", "AlarmReceiver invoked!");
Log.d(LocalNotification.TAG, "AlarmReceiver invoked!");

Bundle bundle = intent.getExtras();
NotificationManager notificationMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Expand Down Expand Up @@ -79,5 +81,18 @@ public void onReceive(Context context, Intent intent) {
// Send JS a message
LocalNotification.getCordovaWebView().sendJavascript("cordova.fireDocumentEvent('receivedLocalNotification', { active : true, notificationId : " + notificationId + " })");
}

try {
String strnotifid = "" + notificationId;
context.getApplicationContext()
.getSharedPreferences(LocalNotification.TAG, Context.MODE_PRIVATE)
.edit()
.remove(strnotifid)
.commit();
Log.d(LocalNotification.TAG, "Notification unpersisted: " + notificationId);
} catch (Exception e) {
Log.e(LocalNotification.TAG, "Failure to unpersist: " + e);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class AlarmRestoreOnBoot extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

// Obtain alarm details form Shared Preferences
SharedPreferences alarmSettings = context.getSharedPreferences(LocalNotification.TAG, Context.MODE_PRIVATE);
final AlarmHelper alarm = new AlarmHelper();
Expand All @@ -34,22 +35,54 @@ public void onReceive(Context context, Intent intent) {
*/
for (String alarmId : allAlarms.keySet()) {
try {
JSONArray alarmDetails = new JSONArray(alarmSettings.getString(alarmId, ""));
AlarmOptions options = new AlarmOptions(alarmDetails, context);

alarm.addAlarm(options.getAlarmTitle(),
options.getAlarmSubTitle(),
options.getAlarmTicker(),
options.getNotificationId(),
options.getIcon(),
options.getCal().getTimeInMillis());

} catch (JSONException e) {
Log.d(LocalNotification.TAG,
"AlarmRestoreOnBoot: Error while restoring alarm details after reboot: " + e.toString());
JSONArray args = new JSONArray(alarmSettings.getString(alarmId, "")); // second parameter is default valu
Log.d(LocalNotification.TAG, "alarmDetails in AlarmRestoreOnBoot.onReceive: " + args.toString());

//long seconds = System.currentTimeMillis() + (args.getJSONObject(1).getLong("seconds") * 1000);
long seconds = args.getJSONObject(1).getLong("seconds");
String title, ticker, icon, message;
int iconResource = android.R.drawable.btn_star_big_on;

title = ticker = icon = message = "";
try {
title = args.getJSONObject(1).getString("title");
} catch (Exception e){
title = "Notification";
}
try {
message = args.getJSONObject(1).getString("message");
} catch (Exception e){
message = "Notification message";
}
try {
ticker = args.getJSONObject(1).getString("ticker");
} catch (Exception e) {
ticker = message;
}
try {
icon = args.getJSONObject(1).getString("icon");
} catch (Exception e) {}


if (icon != "") {
try {
iconResource = android.R.drawable.btn_star_big_on;
//iconResource = cordova.getActivity().getResources().getIdentifier(icon, "drawable", cordova.getActivity().getPackageName());
} catch(Exception e) {
Log.e(LocalNotification.TAG, "The icon resource couldn't be found. Taking default icon.");
}
}

alarm.addAlarm(title, message, ticker, alarmId, iconResource, seconds);


} catch (Exception e) {
Log.e(LocalNotification.TAG, "AlarmRestoreOnBoot: Error while restoring alarm details after reboot: " + e.toString());
}

Log.d(LocalNotification.TAG, "AlarmRestoreOnBoot: Successfully restored alarms upon reboot");
Log.d(LocalNotification.TAG, "AlarmRestoreOnBoot: Successfully restored alarms id upon reboot: " + alarmId);

}
Log.d(LocalNotification.TAG, "AlarmRestoreOnBoot: Successfully restored alarms upon reboot");
}
}
Loading