Skip to content

Delete Alarms when intent is deleted #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/src/windows/Microsoft.Toolkit.Uwp.Notifications.*
/src/windows/**/.vs
/src/windows/**/bin
/src/windows/**/obj
/src/windows/**/obj
*.DS_Store
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,12 @@ cordova.plugins.notification.local.requestIgnoreBatteryOptimizations(function (g
```

The request method here will work one of two ways.
1. If you have the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission defined in the manifest, it will use ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to explicitly ignore battery optimizations for this app. This is the best overall user experience, but the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission seems to be frowned upon and can get your app banned. This plugin does not have this permission in plugin.xml for this reason, so you will need to use the cordova-custom-config plugin to add it to your config.xml
1. If you have the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission defined in the manifest, it will use ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS to explicitly ignore battery optimizations for this app. This is the best overall user experience, but the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission seems to be frowned upon and can get your app banned. This plugin does not have this permission in plugin.xml for this reason, so you will need to use the cordova-custom-config plugin to add it to your config.xml. Alternatively, you can use the edit-config tag in the platform section of the config.xml.
```xml
<edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/uses-permission" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
</edit-config>
```
2. If you do not have REQUEST_IGNORE_BATTERY_OPTIMIZATIONS requested, it will launch ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS to show a list of all applications. You will want to put some sort of instructions prior to this to walk the user through this. Also, this action doesn't exist on all Android devices (is missing on Samsung phones), which will make this method simply return false if it can't start the activity.


Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-local-notification"
version="0.9.0-beta.4">
version="0.9.0-beta.5">

<name>LocalNotification</name>

Expand Down
11 changes: 10 additions & 1 deletion src/android/notification/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import de.appplant.cordova.plugin.localnotification.TriggerReceiver;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
Expand Down Expand Up @@ -97,6 +98,9 @@ public enum Type {
// Builder with full configuration
private final NotificationCompat.Builder builder;

// Receiver to handle the trigger event
private Class<?> receiver = TriggerReceiver.class;

/**
* Constructor
*
Expand Down Expand Up @@ -184,6 +188,8 @@ void schedule(Request request, Class<?> receiver) {
List<Pair<Date, Intent>> intents = new ArrayList<Pair<Date, Intent>>();
Set<String> ids = new ArraySet<String>();
AlarmManager mgr = getAlarmMgr();

this.receiver = receiver;

cancelScheduledAlarms();

Expand Down Expand Up @@ -311,7 +317,8 @@ private void cancelScheduledAlarms() {
return;

for (String action : actions) {
Intent intent = new Intent(action);
Intent intent = new Intent(context, this.receiver)
.setAction(action);

PendingIntent pi = PendingIntent.getBroadcast(
context, 0, intent, 0);
Expand Down Expand Up @@ -348,6 +355,8 @@ void update (JSONObject updates, Class<?> receiver) {
mergeJSONObjects(updates);
persist(null);

this.receiver = receiver;

if (getType() != Type.TRIGGERED)
return;

Expand Down
2 changes: 1 addition & 1 deletion www/local-notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ exports._mergeWithDefaults = function (options) {

options.meta = {
plugin: 'cordova-plugin-local-notification',
version: '0.9-beta.4'
version: '0.9-beta.5'
};

return options;
Expand Down