Skip to content

Commit

Permalink
[0.1.4] FIX - Make the notification clickable on API 31+ by replacing…
Browse files Browse the repository at this point in the history
… the broadcast receiver by an activity used to start the main cordova activity.
  • Loading branch information
bhandaribhumin committed Nov 2, 2022
1 parent 038f36f commit 0b6aefe
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 110 deletions.
23 changes: 19 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
ChangeLog
---------
#### Version 0.9.0-beta.5
### Vrsion 0.1.4 (2.11.2022)

- FIX Bug 🐛 :bug
- Make notifications clickable on API 31+ by replacing the broadcast receiver by an activity used to start the main cordova activity.

#### Important notice

If the app is in background, it must not be launched but put in foreground.
To avoid launching the app in this case, add the following in your config.xml file:
`<preference name="AndroidLaunchMode" value="singleInstance"/>`
## Special Thanks
https://github.com/powowbox/cordova-plugin-local-notification-12

#### Version 0.1.3
- FIX Bug 🐛 :bug
- Added missing 'PendingIntent.FLAG_MUTABLE' and fixed gradle
- Guard against webview crash
- Add thread identifier property
- Delete Alarms when intent is deleted
- Not calling delegate events if nil or if we're consuming the notifiction
- Android 13 `POST_NOTIFICATIONS ` prmission and runtime popup added
- Enhancements (Android)
- New interfaces to ask for / register permissions required to schedule local notifications
- New method addded for android `setDummyNotification()`
- Enhancements (Android)
- New interfaces to ask for / register permissions required to schedule local notifications
- New method addded for android `setDummyNotification()`

#### Version 0.9.0-beta.4
- Platform enhancements
Expand Down Expand Up @@ -159,3 +172,5 @@ Please also read the [Upgrade Guide](https://github.com/bhandaribhumin/cordova-p
### Version 0.8.0 (05.03.2015)

Added condition to get view from view or engine [PR](https://github.com/bhandaribhumin/cordova-plugin-local-notification-12/pull/1)


15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@

## Important Notice

If the app is in background, it must not be launched but put in foreground.
To avoid launching the app in this case, add the following in your config.xml file:
`<preference name="AndroidLaunchMode" value="singleInstance"/>`

Please make sure that you always read the tagged README for the version you're using.

See the _0.8_ branch if you cannot upgrade. Further development for `v0.9-beta` will happen here. The `0.9-dev` and `ios10` branches are obsolate and will be removed soon.
Expand All @@ -64,6 +68,11 @@ __Known issues__

Please report bugs or missing features!

## Install
```bash
npm i cordova-plugin-local-notification-12
cordova plugin add cordova-plugin-local-notification-12
```

## Basics

Expand Down Expand Up @@ -544,11 +553,13 @@ See the sample app for how to use them.

| Method | Method | Method | Method | Method | Method |
| :------- | :---------------- | :-------------- | :------------- | :------------ | :--------------- |
| schedule | cancelAll | isTriggered | get | removeActions | un |
| schedule | cancelAll | isTriggered | get | removeActions | un |
| update | hasPermission | getType | getAll | hasActions | fireQueuedEvents |
| clear | requestPermission | getIds | getScheduled | getDefaults | requestDoNotDisturbPermissions |
| clearAll | isPresent | getScheduledIds | getTriggered | setDefaults | hasDoNotDisturbPermissions |
| cancel | isScheduled | getTriggeredIds | addActions | on |
| setDummyNotifications |


## SetDummyNotification

Expand Down Expand Up @@ -606,3 +617,5 @@ Made with :yum: from Leipzig
[appplant]: http://appplant.de

FIX: Added condition to get view from view or engine


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-plugin-local-notification-12",
"version": "0.1.3",
"version": "0.1.4",
"description": "Schedules and queries for local notifications",
"cordova": {
"id": "cordova-plugin-local-notification-12",
Expand Down
11 changes: 8 additions & 3 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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-12"
version="0.1.3">
version="0.1.4">

<name>LocalNotification</name>

Expand Down Expand Up @@ -124,9 +124,10 @@
android:name="de.appplant.cordova.plugin.localnotification.ClearReceiver"
android:exported="false" />

<service
<activity
android:name="de.appplant.cordova.plugin.localnotification.ClickReceiver"
android:exported="false" />
android:exported="false"
android:theme="@style/Theme.AppCompat.NoActionBar" />

<receiver
android:name="de.appplant.cordova.plugin.localnotification.RestoreReceiver"
Expand Down Expand Up @@ -199,6 +200,10 @@
src="src/android/notification/receiver/AbstractTriggerReceiver.java"
target-dir="src/de/appplant/cordova/plugin/notification/receiver" />

<source-file
src="src/android/notification/receiver/NotificationTrampolineActivity.java"
target-dir="src/de/appplant/cordova/plugin/notification/receiver" />

<source-file
src="src/android/notification/trigger/DateTrigger.java"
target-dir="src/de/appplant/cordova/plugin/notification/trigger" />
Expand Down
7 changes: 5 additions & 2 deletions src/android/TriggerReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ public boolean checkAppRunning() {
*/
@Override
public Notification buildNotification(Builder builder, Bundle bundle) {
return builder.setClickActivity(ClickReceiver.class).setClearReceiver(ClearReceiver.class).setExtras(bundle)
.build();
return builder
.setClickActivity(ClickReceiver.class)
.setClearReceiver(ClearReceiver.class)
.setExtras(bundle)
.build();
}

}
64 changes: 13 additions & 51 deletions src/android/notification/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
import android.graphics.Canvas;

import java.util.List;
import java.util.Random;

import de.appplant.cordova.plugin.notification.action.Action;
import de.appplant.cordova.plugin.notification.util.LaunchUtils;

import static de.appplant.cordova.plugin.notification.Notification.EXTRA_UPDATE;

Expand All @@ -58,9 +58,6 @@ public final class Builder {
// Notification options passed by JS
private final Options options;

// To generate unique request codes
private final Random random = new Random();

// Receiver to handle the clear event
private Class<?> clearReceiver;

Expand Down Expand Up @@ -200,17 +197,7 @@ void applyFullScreenIntent(NotificationCompat.Builder builder) {
.getLaunchIntentForPackage(pkgName)
.putExtra("launchNotificationId", options.getId());

int reqCode = random.nextInt();
// request code and flags not added for demo purposes
int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT <= 30) {
// null
}else{
flags = 33554432 | PendingIntent.FLAG_UPDATE_CURRENT;
}

PendingIntent pendingIntent = PendingIntent.getActivity(context, reqCode, intent, flags);

PendingIntent pendingIntent = LaunchUtils.getActivityPendingIntent(context, intent);
builder.setFullScreenIntent(pendingIntent, true);
}

Expand Down Expand Up @@ -402,18 +389,7 @@ private void applyDeleteReceiver(NotificationCompat.Builder builder) {
intent.putExtras(extras);
}

int reqCode = random.nextInt();

int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT <= 30) {
// null
}else{
flags = 33554432 | PendingIntent.FLAG_UPDATE_CURRENT;
}

PendingIntent deleteIntent = PendingIntent.getBroadcast(
context, reqCode, intent, flags);

PendingIntent deleteIntent = LaunchUtils.getBroadcastPendingIntent(context, intent);
builder.setDeleteIntent(deleteIntent);
}

Expand All @@ -428,6 +404,13 @@ private void applyContentReceiver(NotificationCompat.Builder builder) {
if (clickActivity == null)
return;

Action[] actions = options.getActions();
if (actions != null && actions.length > 0 ) {
// if actions are defined, the user must click on button actions to launch the app.
// Don't make the notification clickable in this case
return;
}

Intent intent = new Intent(context, clickActivity)
.putExtra(Notification.EXTRA_ID, options.getId())
.putExtra(Action.EXTRA_ID, Action.CLICK_ACTION_ID)
Expand All @@ -438,18 +421,7 @@ private void applyContentReceiver(NotificationCompat.Builder builder) {
intent.putExtras(extras);
}

int reqCode = random.nextInt();

int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT <= 30) {
// null
}else{
flags = 33554432 | PendingIntent.FLAG_UPDATE_CURRENT;
}

PendingIntent contentIntent = PendingIntent.getService(
context, reqCode, intent, flags);

PendingIntent contentIntent = LaunchUtils.getTaskStackPendingIntent(context, intent);
builder.setContentIntent(contentIntent);
}

Expand Down Expand Up @@ -495,17 +467,7 @@ private PendingIntent getPendingIntentForAction (Action action) {
intent.putExtras(extras);
}

int reqCode = random.nextInt();

int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT <= 30) {
// null
}else{
flags = 33554432 | PendingIntent.FLAG_UPDATE_CURRENT;
}

return PendingIntent.getService(
context, reqCode, intent, flags);
return LaunchUtils.getTaskStackPendingIntent(context, intent);
}

/**
Expand All @@ -531,4 +493,4 @@ private NotificationCompat.Builder findOrCreateBuilder() {
return builder;
}

}
}
33 changes: 5 additions & 28 deletions src/android/notification/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.net.Uri;
import android.service.notification.StatusBarNotification;
import android.util.Pair;
Expand All @@ -44,12 +43,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import androidx.collection.ArraySet;
import androidx.core.app.NotificationCompat;

Expand All @@ -60,7 +53,8 @@
import static androidx.core.app.NotificationCompat.PRIORITY_HIGH;
import static androidx.core.app.NotificationCompat.PRIORITY_MAX;
import static androidx.core.app.NotificationCompat.PRIORITY_MIN;
import static java.lang.Thread.sleep;

import de.appplant.cordova.plugin.notification.util.LaunchUtils;

/**
* Wrapper class around OS notification class. Handles basic operations
Expand Down Expand Up @@ -225,14 +219,7 @@ void schedule(Request request, Class<?> receiver) {
if (!date.after(new Date()) && trigger(intent, receiver))
continue;

int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT <= 30) {
// null
}else{
flags = 33554432 | PendingIntent.FLAG_UPDATE_CURRENT;
}
PendingIntent pi = PendingIntent.getBroadcast(
context, 0, intent, flags);
PendingIntent pi = LaunchUtils.getBroadcastPendingIntent(context, intent);

try {
switch (options.getPrio()) {
Expand Down Expand Up @@ -318,17 +305,7 @@ private void cancelScheduledAlarms() {

for (String action : actions) {
Intent intent = new Intent(action);

int flags = PendingIntent.FLAG_UPDATE_CURRENT;
if (android.os.Build.VERSION.SDK_INT <= 30) {
// null
}else{
flags = 33554432 | PendingIntent.FLAG_UPDATE_CURRENT;
}

PendingIntent pi = PendingIntent.getBroadcast(
context, 0, intent, flags);

PendingIntent pi = LaunchUtils.getBroadcastPendingIntent(context, intent);
if (pi != null) {
getAlarmMgr().cancel(pi);
}
Expand Down Expand Up @@ -513,4 +490,4 @@ private AlarmManager getAlarmMgr () {
return (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
}

}
}

0 comments on commit 0b6aefe

Please sign in to comment.