Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 89ea506

Browse files
Merge pull request #1466 from lukebars/master
Added method getScheduledLocalNotifications()
2 parents 8c4a4fc + 1c7a196 commit 89ea506

File tree

13 files changed

+161
-29
lines changed

13 files changed

+161
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
4646

4747
- (Android) Sounds are playing even in Do Not Disturb [#1432](https://github.com/zo0r/react-native-push-notification/issues/1432#issuecomment-633367111)
4848
- (Android) onNotification fires every time when the app goes from background to foreground [#1455](https://github.com/zo0r/react-native-push-notification/issues/1455)
49-
- (Android) Cannot send to notification centre because there is no 'message' field in: Bundle [#1452](https://github.com/zo0r/react-native-push-notification/issues/1452)
49+
- (Android) java.lang.NullPointerException: Attempt to invoke virtual method 'void com.dieam.reactnativepushnotification.modules.d.c(android.os.Bundle)' on a null object reference [#1431](https://github.com/zo0r/react-native-push-notification/issues/1431#issuecomment-633315150)
5050

5151
## [3.5.1] - 2020-05-20
5252

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,32 @@ Removes the specified notifications from Notification Center
524524
| ----------- | ----- | -------- | ---------------------------------- |
525525
| identifiers | array | Yes | Array of notification identifiers. |
526526

527+
### 6) getScheduledLocalNotifications
528+
529+
```javascript
530+
PushNotificationIOS.getScheduledLocalNotifications(callback);
531+
```
532+
533+
Provides you with a list of the app’s scheduled local notifications that are yet to be displayed
534+
535+
**Parameters:**
536+
537+
| Name | Type | Required | Description |
538+
| -------- | -------- | -------- | ----------------------------------------------------------- |
539+
| callback | function | Yes | Function which receive an array of delivered notifications. |
540+
541+
Returns an array of local scheduled notification objects containing:
542+
543+
| Name | Type | Description |
544+
| -------- | -------- | -------- | ----------------------------------------------------------- |
545+
| id | number | The identifier of this notification. |
546+
| date | Date | The fire date of this notification. |
547+
| title | string | The title of this notification. |
548+
| message | string | The message body of this notification. |
549+
| soundName | string | The sound name of this notification. |
550+
| repeatInterval | number | The repeat interval of this notification. |
551+
| number | number | App notification badge count number. |
552+
527553
## Abandon Permissions
528554

529555
`PushNotification.abandonPermissions()` Revokes the current token and unregister for all remote notifications received via APNS or FCM.

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,15 @@ public void removeAllDeliveredNotifications() {
233233
* Returns a list of all notifications currently in the Notification Center
234234
*/
235235
public void getDeliveredNotifications(Callback callback) {
236-
callback.invoke(mRNPushNotificationHelper.getDeliveredNotifications());
236+
callback.invoke(mRNPushNotificationHelper.getDeliveredNotifications());
237+
}
238+
239+
@ReactMethod
240+
/**
241+
* Returns a list of all currently scheduled notifications
242+
*/
243+
public void getScheduledLocalNotifications(Callback callback) {
244+
callback.invoke(mRNPushNotificationHelper.getScheduledLocalNotifications());
237245
}
238246

239247
@ReactMethod

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,26 @@ public String getId() {
330330
return id;
331331
}
332332

333+
public String getSound() {
334+
return sound;
335+
}
336+
337+
public String getMessage() {
338+
return message;
339+
}
340+
341+
public String getTitle() {
342+
return title;
343+
}
344+
345+
public String getNumber() {
346+
return number;
347+
}
348+
349+
public String getRepeatType() {
350+
return repeatType;
351+
}
352+
333353
public double getFireDate() {
334354
return fireDate;
335355
}

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import androidx.core.app.NotificationCompat;
2929

3030
import com.facebook.react.bridge.Arguments;
31-
import com.facebook.react.bridge.ReactContext;
3231
import com.facebook.react.bridge.ReadableArray;
3332
import com.facebook.react.bridge.ReadableMap;
3433
import com.facebook.react.bridge.WritableArray;
@@ -43,6 +42,7 @@
4342
import java.util.Date;
4443
import java.util.GregorianCalendar;
4544
import java.util.List;
45+
import java.util.Map;
4646

4747
import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
4848
import static com.dieam.reactnativepushnotification.modules.RNPushNotificationAttributes.fromJson;
@@ -167,14 +167,14 @@ public void sendNotificationScheduledCore(Bundle bundle) {
167167
// notification to the user
168168
PendingIntent pendingIntent = toScheduleNotificationIntent(bundle);
169169

170-
if(pendingIntent == null) {
170+
if (pendingIntent == null) {
171171
return;
172172
}
173173

174174
Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
175175
bundle.getString("id"), Long.toString(fireDate)));
176176
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
177-
if(allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
177+
if (allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
178178
getAlarmManager().setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
179179
} else {
180180
getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
@@ -294,7 +294,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
294294
final String visibilityString = bundle.getString("visibility");
295295

296296
if (visibilityString != null) {
297-
switch(visibilityString.toLowerCase()) {
297+
switch (visibilityString.toLowerCase()) {
298298
case "private":
299299
visibility = NotificationCompat.VISIBILITY_PRIVATE;
300300
break;
@@ -429,9 +429,9 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
429429

430430
if (!bundle.containsKey("playSound") || bundle.getBoolean("playSound")) {
431431
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
432-
432+
433433
String soundName = bundle.getString("soundName");
434-
434+
435435
if (soundName != null) {
436436
if (!"default".equalsIgnoreCase(soundName)) {
437437

@@ -493,7 +493,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
493493
long vibration = bundle.containsKey("vibration") ? (long) bundle.getDouble("vibration") : DEFAULT_VIBRATION;
494494
if (vibration == 0)
495495
vibration = DEFAULT_VIBRATION;
496-
496+
497497
channel_id = channel_id + "-" + vibration;
498498

499499
vibratePattern = new long[]{0, vibration};
@@ -582,7 +582,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
582582
if (!(this.isApplicationInForeground(context) && bundle.getBoolean("ignoreInForeground"))) {
583583
Notification info = notification.build();
584584
info.defaults |= Notification.DEFAULT_LIGHTS;
585-
585+
586586
if (bundle.containsKey("tag")) {
587587
String tag = bundle.getString("tag");
588588
notificationManager.notify(tag, notificationID, info);
@@ -726,6 +726,33 @@ public WritableArray getDeliveredNotifications() {
726726

727727
}
728728

729+
public WritableArray getScheduledLocalNotifications() {
730+
WritableArray scheduled = Arguments.createArray();
731+
732+
Map<String, ?> scheduledNotifications = scheduledNotificationsPersistence.getAll();
733+
734+
for (Map.Entry<String, ?> entry : scheduledNotifications.entrySet()) {
735+
try {
736+
RNPushNotificationAttributes notification = fromJson(entry.getValue().toString());
737+
WritableMap notificationMap = Arguments.createMap();
738+
739+
notificationMap.putString("title", notification.getTitle());
740+
notificationMap.putString("message", notification.getMessage());
741+
notificationMap.putString("number", notification.getNumber());
742+
notificationMap.putDouble("date", notification.getFireDate());
743+
notificationMap.putString("id", notification.getId());
744+
notificationMap.putString("repeatInterval", notification.getRepeatType());
745+
notificationMap.putString("soundName", notification.getSound());
746+
747+
scheduled.pushMap(notificationMap);
748+
} catch (JSONException e) {
749+
Log.e(LOG_TAG, e.getMessage());
750+
}
751+
}
752+
753+
return scheduled;
754+
}
755+
729756
public void cancelAllScheduledNotifications() {
730757
Log.i(LOG_TAG, "Cancelling all notifications");
731758

@@ -758,7 +785,7 @@ private void cancelScheduledNotification(String notificationIDString) {
758785
b.putString("id", notificationIDString);
759786
PendingIntent pendingIntent = toScheduleNotificationIntent(b);
760787

761-
if(pendingIntent != null) {
788+
if (pendingIntent != null) {
762789
getAlarmManager().cancel(pendingIntent);
763790
}
764791

@@ -888,8 +915,8 @@ public boolean isApplicationInForeground(Context context) {
888915
if (processInfos != null) {
889916
for (RunningAppProcessInfo processInfo : processInfos) {
890917
if (processInfo.processName.equals(context.getPackageName())
891-
&& processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
892-
&& processInfo.pkgList.length > 0) {
918+
&& processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
919+
&& processInfo.pkgList.length > 0) {
893920
return true;
894921
}
895922
}

component/index.android.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import { NativeModules, DeviceEventEmitter } from "react-native";
44

5-
var RNPushNotification = NativeModules.RNPushNotification;
6-
var _notifHandlers = new Map();
5+
let RNPushNotification = NativeModules.RNPushNotification;
6+
let _notifHandlers = new Map();
77

88
var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
99
var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
1010
var NOTIF_ACTION_EVENT = 'notificationActionReceived';
1111
var REMOTE_FETCH_EVENT = 'remoteFetch';
1212

13-
var NotificationsComponent = function() {
13+
let NotificationsComponent = function() {
1414

1515
};
1616

@@ -64,13 +64,13 @@ NotificationsComponent.prototype.checkPermissions = function(callback) {
6464
};
6565

6666
NotificationsComponent.prototype.addEventListener = function(type, handler) {
67-
var listener;
67+
let listener;
6868
if (type === 'notification') {
6969
listener = DeviceEventEmitter.addListener(
7070
DEVICE_NOTIF_EVENT,
7171
function(notifData) {
7272
if (notifData && notifData.dataJSON) {
73-
var data = JSON.parse(notifData.dataJSON);
73+
let data = JSON.parse(notifData.dataJSON);
7474
handler(data);
7575
}
7676
}
@@ -87,7 +87,7 @@ NotificationsComponent.prototype.addEventListener = function(type, handler) {
8787
REMOTE_FETCH_EVENT,
8888
function(notifData) {
8989
if (notifData && notifData.dataJSON) {
90-
var notificationData = JSON.parse(notifData.dataJSON)
90+
let notificationData = JSON.parse(notifData.dataJSON)
9191
handler(notificationData);
9292
}
9393
}
@@ -108,7 +108,7 @@ NotificationsComponent.prototype.addEventListener = function(type, handler) {
108108
};
109109

110110
NotificationsComponent.prototype.removeEventListener = function(type, handler) {
111-
var listener = _notifHandlers.get(type);
111+
let listener = _notifHandlers.get(type);
112112
if (!listener) {
113113
return;
114114
}
@@ -131,6 +131,9 @@ NotificationsComponent.prototype.removeAllDeliveredNotifications = function() {
131131
NotificationsComponent.prototype.getDeliveredNotifications = function(callback) {
132132
RNPushNotification.getDeliveredNotifications(callback);
133133
}
134+
NotificationsComponent.prototype.getScheduledLocalNotifications = function(callback) {
135+
RNPushNotification.getScheduledLocalNotifications(callback);
136+
}
134137
NotificationsComponent.prototype.removeDeliveredNotifications = function(identifiers) {
135138
RNPushNotification.removeDeliveredNotifications(identifiers);
136139
}

example/App.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ export default class App extends Component {
105105
}}>
106106
<Text>Abandon Permissions</Text>
107107
</TouchableOpacity>
108+
<TouchableOpacity
109+
style={styles.button}
110+
onPress={() => {
111+
this.notif.getScheduledLocalNotifications(notifs => console.log(notifs));
112+
}}>
113+
<Text>Console.Log Scheduled Local Notifications</Text>
114+
</TouchableOpacity>
108115

109116
<View style={styles.spacer}></View>
110117

example/NotifService.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,8 @@ export default class NotifService {
109109
abandonPermissions() {
110110
PushNotification.abandonPermissions();
111111
}
112+
113+
getScheduledLocalNotifications(callback) {
114+
PushNotification.getScheduledLocalNotifications(callback);
115+
}
112116
}

example/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ PODS:
292292
- React-cxxreact (= 0.62.2)
293293
- React-jsi (= 0.62.2)
294294
- ReactCommon/callinvoker (= 0.62.2)
295-
- RNCPushNotificationIOS (1.1.1):
295+
- RNCPushNotificationIOS (1.2.2):
296296
- React
297297
- Yoga (1.14.0)
298298
- YogaKit (1.18.1):
@@ -453,10 +453,10 @@ SPEC CHECKSUMS:
453453
React-RCTText: fae545b10cfdb3d247c36c56f61a94cfd6dba41d
454454
React-RCTVibration: 4356114dbcba4ce66991096e51a66e61eda51256
455455
ReactCommon: ed4e11d27609d571e7eee8b65548efc191116eb3
456-
RNCPushNotificationIOS: a0b6894f4ad9b93d9dac467fdf4d055660ac8a0d
456+
RNCPushNotificationIOS: 4c97a36dbec42dba411cc35e6dac25e34a805fde
457457
Yoga: 3ebccbdd559724312790e7742142d062476b698e
458458
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a
459459

460460
PODFILE CHECKSUM: 56c2537f71f3f02200d6918c542a8e89a0b422fa
461461

462-
COCOAPODS: 1.9.1
462+
COCOAPODS: 1.9.2

example/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
"ios": "react-native run-ios",
88
"start": "react-native start",
99
"test": "jest",
10-
"lint": "eslint ."
10+
"lint": "eslint .",
11+
"pod-install": "cd ios && pod install"
1112
},
1213
"dependencies": {
13-
"@react-native-community/push-notification-ios": "^1.1.1",
14+
"@react-native-community/push-notification-ios": "^1.2.2",
1415
"react": "16.11.0",
1516
"react-native": "0.62.2",
1617
"react-native-push-notification": "git+https://[email protected]/zo0r/react-native-push-notification.git"

index.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var RNNotificationsComponent = require( './component' );
1010

1111
var RNNotifications = RNNotificationsComponent.component;
1212

13-
var Platform = require('react-native').Platform;
13+
let Platform = require('react-native').Platform;
1414

1515
var Notifications = {
1616
handler: RNNotifications,
@@ -439,6 +439,42 @@ Notifications.getDeliveredNotifications = function() {
439439
return this.callNative('getDeliveredNotifications', arguments);
440440
}
441441

442+
Notifications.getScheduledLocalNotifications = function(callback) {
443+
const mapNotifications = (notifications) => {
444+
let mappedNotifications = [];
445+
if(notifications?.length > 0) {
446+
if(Platform.OS === 'ios'){
447+
mappedNotifications = notifications.map(notif => {
448+
return ({
449+
soundName: notif.soundName,
450+
repeatInterval: notif.repeatInterval,
451+
id: notif.userInfo?.id,
452+
date: new Date(notif.fireDate),
453+
number: notif?.applicationIconBadgeNumber,
454+
message: notif?.alertBody,
455+
title: notif?.alertTitle,
456+
})
457+
})
458+
} else if(Platform.OS === 'android') {
459+
mappedNotifications = notifications.map(notif => {
460+
return ({
461+
soundName: notif.soundName,
462+
repeatInterval: notif.repeatInterval,
463+
id: notif.id,
464+
date: new Date(notif.date),
465+
number: notif.number,
466+
message: notif.message,
467+
title: notif.title,
468+
})
469+
})
470+
}
471+
}
472+
callback(mappedNotifications);
473+
}
474+
475+
return this.callNative('getScheduledLocalNotifications', [mapNotifications]);
476+
}
477+
442478
Notifications.removeDeliveredNotifications = function() {
443479
return this.callNative('removeDeliveredNotifications', arguments);
444480
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"url": "git+ssh://[email protected]:zo0r/react-native-push-notification.git"
2525
},
2626
"dependencies": {
27-
"@react-native-community/push-notification-ios": "^1.2.0"
27+
"@react-native-community/push-notification-ios": "^1.2.2"
2828
},
2929
"peerDependencies": {
3030
"react-native": ">=0.33"

0 commit comments

Comments
 (0)