Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
getScheduledLocalNotifications for iOS and android
  • Loading branch information
lukebars committed Jun 5, 2020
commit 3ecb5ec1273dc121835cf9e26c5d2339a1919935
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,15 @@ public void removeAllDeliveredNotifications() {
* Returns a list of all notifications currently in the Notification Center
*/
public void getDeliveredNotifications(Callback callback) {
callback.invoke(mRNPushNotificationHelper.getDeliveredNotifications());
callback.invoke(mRNPushNotificationHelper.getDeliveredNotifications());
}

@ReactMethod
/**
* Returns a list of all currently scheduled notifications
*/
public void getScheduledLocalNotifications(Callback callback) {
callback.invoke(mRNPushNotificationHelper.getScheduledLocalNotifications());
}

@ReactMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.os.Bundle;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.facebook.react.bridge.Arguments;
Expand All @@ -40,7 +41,7 @@
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;
import java.util.Map;

import static com.dieam.reactnativepushnotification.modules.RNPushNotification.LOG_TAG;
import static com.dieam.reactnativepushnotification.modules.RNPushNotificationAttributes.fromJson;
Expand Down Expand Up @@ -143,14 +144,14 @@ public void sendNotificationScheduledCore(Bundle bundle) {
// notification to the user
PendingIntent pendingIntent = toScheduleNotificationIntent(bundle);

if(pendingIntent == null) {
if (pendingIntent == null) {
return;
}

Log.d(LOG_TAG, String.format("Setting a notification with id %s at time %s",
bundle.getString("id"), Long.toString(fireDate)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if(allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (allowWhileIdle && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getAlarmManager().setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
} else {
getAlarmManager().setExact(AlarmManager.RTC_WAKEUP, fireDate, pendingIntent);
Expand Down Expand Up @@ -195,7 +196,7 @@ public void sendToNotificationCentre(Bundle bundle) {
final String priorityString = bundle.getString("priority");

if (priorityString != null) {
switch(priorityString.toLowerCase()) {
switch (priorityString.toLowerCase()) {
case "max":
priority = NotificationCompat.PRIORITY_MAX;
break;
Expand All @@ -217,43 +218,43 @@ public void sendToNotificationCentre(Bundle bundle) {
}

int importance = NotificationManager.IMPORTANCE_HIGH;
final String importanceString = bundle.getString("importance");

if (importanceString != null) {
switch(importanceString.toLowerCase()) {
case "default":
importance = NotificationManager.IMPORTANCE_DEFAULT;
break;
case "max":
importance = NotificationManager.IMPORTANCE_MAX;
break;
case "high":
importance = NotificationManager.IMPORTANCE_HIGH;
break;
case "low":
importance = NotificationManager.IMPORTANCE_LOW;
break;
case "min":
importance = NotificationManager.IMPORTANCE_MIN;
break;
case "none":
importance = NotificationManager.IMPORTANCE_NONE;
break;
case "unspecified":
importance = NotificationManager.IMPORTANCE_UNSPECIFIED;
break;
default:
importance = NotificationManager.IMPORTANCE_HIGH;
}
}
final String importanceString = bundle.getString("importance");

if (importanceString != null) {
switch (importanceString.toLowerCase()) {
case "default":
importance = NotificationManager.IMPORTANCE_DEFAULT;
break;
case "max":
importance = NotificationManager.IMPORTANCE_MAX;
break;
case "high":
importance = NotificationManager.IMPORTANCE_HIGH;
break;
case "low":
importance = NotificationManager.IMPORTANCE_LOW;
break;
case "min":
importance = NotificationManager.IMPORTANCE_MIN;
break;
case "none":
importance = NotificationManager.IMPORTANCE_NONE;
break;
case "unspecified":
importance = NotificationManager.IMPORTANCE_UNSPECIFIED;
break;
default:
importance = NotificationManager.IMPORTANCE_HIGH;
}
}

channel_id = channel_id + "-" + importance;

int visibility = NotificationCompat.VISIBILITY_PRIVATE;
final String visibilityString = bundle.getString("visibility");

if (visibilityString != null) {
switch(visibilityString.toLowerCase()) {
switch (visibilityString.toLowerCase()) {
case "private":
visibility = NotificationCompat.VISIBILITY_PRIVATE;
break;
Expand All @@ -274,12 +275,12 @@ public void sendToNotificationCentre(Bundle bundle) {
.setVisibility(visibility)
.setPriority(priority)
.setAutoCancel(bundle.getBoolean("autoCancel", true));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // API 26 and higher
// Changing Default mode of notification
notification.setDefaults(Notification.DEFAULT_LIGHTS);
}

String group = bundle.getString("group");

if (group != null) {
Expand Down Expand Up @@ -350,9 +351,9 @@ public void sendToNotificationCentre(Bundle bundle) {

if (!bundle.containsKey("playSound") || bundle.getBoolean("playSound")) {
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

String soundName = bundle.getString("soundName");

if (soundName != null) {
if (!"default".equalsIgnoreCase(soundName)) {

Expand Down Expand Up @@ -414,7 +415,7 @@ public void sendToNotificationCentre(Bundle bundle) {
long vibration = bundle.containsKey("vibration") ? (long) bundle.getDouble("vibration") : DEFAULT_VIBRATION;
if (vibration == 0)
vibration = DEFAULT_VIBRATION;

channel_id = channel_id + "-" + vibration;

vibratePattern = new long[]{0, vibration};
Expand Down Expand Up @@ -480,7 +481,7 @@ public void sendToNotificationCentre(Bundle bundle) {
if (!(this.isApplicationInForeground(context) && bundle.getBoolean("ignoreInForeground"))) {
Notification info = notification.build();
info.defaults |= Notification.DEFAULT_LIGHTS;

if (bundle.containsKey("tag")) {
String tag = bundle.getString("tag");
notificationManager.notify(tag, notificationID, info);
Expand Down Expand Up @@ -585,39 +586,59 @@ public void clearNotification(int notificationID) {
}

public void clearDeliveredNotifications(ReadableArray identifiers) {
NotificationManager notificationManager = notificationManager();
for (int index = 0; index < identifiers.size(); index++) {
String id = identifiers.getString(index);
Log.i(LOG_TAG, "Removing notification with id " + id);
notificationManager.cancel(Integer.parseInt(id));
}
NotificationManager notificationManager = notificationManager();
for (int index = 0; index < identifiers.size(); index++) {
String id = identifiers.getString(index);
Log.i(LOG_TAG, "Removing notification with id " + id);
notificationManager.cancel(Integer.parseInt(id));
}
}

public WritableArray getDeliveredNotifications() {
NotificationManager notificationManager = notificationManager();
StatusBarNotification delivered[] = notificationManager.getActiveNotifications();
Log.i(LOG_TAG, "Found " + delivered.length + " delivered notifications");
WritableArray result = Arguments.createArray();
/*
* stay consistent to the return structure in
* https://facebook.github.io/react-native/docs/pushnotificationios.html#getdeliverednotifications
* but there is no such thing as a 'userInfo'
*/
for (StatusBarNotification notification : delivered) {
Notification original = notification.getNotification();
Bundle extras = original.extras;
WritableMap notif = Arguments.createMap();
notif.putString("identifier", "" + notification.getId());
notif.putString("title", extras.getString(Notification.EXTRA_TITLE));
notif.putString("body", extras.getString(Notification.EXTRA_TEXT));
notif.putString("tag", notification.getTag());
notif.putString("group", original.getGroup());
result.pushMap(notif);
}

return result;
NotificationManager notificationManager = notificationManager();
StatusBarNotification delivered[] = notificationManager.getActiveNotifications();
Log.i(LOG_TAG, "Found " + delivered.length + " delivered notifications");
WritableArray result = Arguments.createArray();
/*
* stay consistent to the return structure in
* https://facebook.github.io/react-native/docs/pushnotificationios.html#getdeliverednotifications
* but there is no such thing as a 'userInfo'
*/
for (StatusBarNotification notification : delivered) {
Notification original = notification.getNotification();
Bundle extras = original.extras;
WritableMap notif = Arguments.createMap();
notif.putString("identifier", "" + notification.getId());
notif.putString("title", extras.getString(Notification.EXTRA_TITLE));
notif.putString("body", extras.getString(Notification.EXTRA_TEXT));
notif.putString("tag", notification.getTag());
notif.putString("group", original.getGroup());
result.pushMap(notif);
}

return result;

}

public WritableArray getScheduledLocalNotifications() {
WritableArray scheduled = Arguments.createArray();

Map<String, ?> scheduledNotifications = scheduledNotificationsPersistence.getAll();
for (Map.Entry<String, ?> entry : scheduledNotifications.entrySet()) {
try {
RNPushNotificationAttributes notification = fromJson(entry.getValue().toString());
WritableMap notificationMap = Arguments.makeNativeMap(notification.toBundle());

scheduled.pushMap(notificationMap);
} catch (JSONException e) {
Log.e(LOG_TAG, e.getMessage());
}
}

return scheduled;

}

public void cancelAllScheduledNotifications() {
Log.i(LOG_TAG, "Cancelling all notifications");

Expand Down Expand Up @@ -650,7 +671,7 @@ private void cancelScheduledNotification(String notificationIDString) {
b.putString("id", notificationIDString);
PendingIntent pendingIntent = toScheduleNotificationIntent(b);

if(pendingIntent != null) {
if (pendingIntent != null) {
getAlarmManager().cancel(pendingIntent);
}

Expand Down Expand Up @@ -686,18 +707,18 @@ private static void commit(SharedPreferences.Editor editor) {
}

public void checkOrCreateDefaultChannel() {
NotificationManager manager = notificationManager();

int importance = NotificationManager.IMPORTANCE_HIGH;
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// Instanciate a default channel with default sound.
String channel_id_sound = NOTIFICATION_CHANNEL_ID + "-default-" + importance + "-" + DEFAULT_VIBRATION;
checkOrCreateChannel(manager, channel_id_sound, soundUri, importance, new long[] {0, DEFAULT_VIBRATION});

// Instanciate a default channel without sound defined for backward compatibility.
String channel_id_no_sound = NOTIFICATION_CHANNEL_ID + "-" + importance + "-" + DEFAULT_VIBRATION;
checkOrCreateChannel(manager, channel_id_no_sound, null, importance, new long[] {0, DEFAULT_VIBRATION});
NotificationManager manager = notificationManager();

int importance = NotificationManager.IMPORTANCE_HIGH;
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

// Instanciate a default channel with default sound.
String channel_id_sound = NOTIFICATION_CHANNEL_ID + "-default-" + importance + "-" + DEFAULT_VIBRATION;
checkOrCreateChannel(manager, channel_id_sound, soundUri, importance, new long[]{0, DEFAULT_VIBRATION});

// Instanciate a default channel without sound defined for backward compatibility.
String channel_id_no_sound = NOTIFICATION_CHANNEL_ID + "-" + importance + "-" + DEFAULT_VIBRATION;
checkOrCreateChannel(manager, channel_id_no_sound, null, importance, new long[]{0, DEFAULT_VIBRATION});
}

private void checkOrCreateChannel(NotificationManager manager, String channel_id, Uri soundUri, int importance, long[] vibratePattern) {
Expand Down Expand Up @@ -730,15 +751,15 @@ private void checkOrCreateChannel(NotificationManager manager, String channel_id
manager.createNotificationChannel(channel);
}
}

private boolean isApplicationInForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processInfos = activityManager.getRunningAppProcesses();
if (processInfos != null) {
for (RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.processName.equals(context.getPackageName())
&& processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& processInfo.pkgList.length > 0) {
&& processInfo.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& processInfo.pkgList.length > 0) {
return true;
}
}
Expand Down
25 changes: 14 additions & 11 deletions component/index.android.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict';

var {
let {
NativeModules,
DeviceEventEmitter,
} = require('react-native');

var RNPushNotification = NativeModules.RNPushNotification;
var _notifHandlers = new Map();
let RNPushNotification = NativeModules.RNPushNotification;
let _notifHandlers = new Map();

var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
var REMOTE_FETCH_EVENT = 'remoteFetch';
let DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
let NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
let REMOTE_FETCH_EVENT = 'remoteFetch';

var NotificationsComponent = function() {
let NotificationsComponent = function() {

};

Expand Down Expand Up @@ -66,13 +66,13 @@ NotificationsComponent.prototype.checkPermissions = function(callback) {
};

NotificationsComponent.prototype.addEventListener = function(type, handler) {
var listener;
let listener;
if (type === 'notification') {
listener = DeviceEventEmitter.addListener(
DEVICE_NOTIF_EVENT,
function(notifData) {
if (notifData && notifData.dataJSON) {
var data = JSON.parse(notifData.dataJSON);
let data = JSON.parse(notifData.dataJSON);
handler(data);
}
}
Expand All @@ -89,7 +89,7 @@ NotificationsComponent.prototype.addEventListener = function(type, handler) {
REMOTE_FETCH_EVENT,
function(notifData) {
if (notifData && notifData.dataJSON) {
var notificationData = JSON.parse(notifData.dataJSON)
let notificationData = JSON.parse(notifData.dataJSON)
handler(notificationData);
}
}
Expand All @@ -100,7 +100,7 @@ NotificationsComponent.prototype.addEventListener = function(type, handler) {
};

NotificationsComponent.prototype.removeEventListener = function(type, handler) {
var listener = _notifHandlers.get(type);
let listener = _notifHandlers.get(type);
if (!listener) {
return;
}
Expand All @@ -123,6 +123,9 @@ NotificationsComponent.prototype.removeAllDeliveredNotifications = function() {
NotificationsComponent.prototype.getDeliveredNotifications = function(callback) {
RNPushNotification.getDeliveredNotifications(callback);
}
NotificationsComponent.prototype.getScheduledLocalNotifications = function(callback) {
RNPushNotification.getScheduledLocalNotifications(callback);
}
NotificationsComponent.prototype.removeDeliveredNotifications = function(identifiers) {
RNPushNotification.removeDeliveredNotifications(identifiers);
}
Expand Down
Loading