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

Keep interface parity with PushNotificationIOS #909

Merged
merged 3 commits into from
Apr 26, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
Expand Down Expand Up @@ -245,4 +246,30 @@ public void clearLocalNotification(int notificationID) {
public void registerNotificationActions(ReadableArray actions) {
registerNotificationsReceiveNotificationActions(actions);
}

@ReactMethod
/**
* Clears all notifications from the notification center
*
*/
public void removeAllDeliveredNotifications() {
mRNPushNotificationHelper.clearNotifications();
}

@ReactMethod
/**
* Returns a list of all notifications currently in the Notification Center
*/
public void getDeliveredNotifications(Callback callback) {
callback.invoke(mRNPushNotificationHelper.getDeliveredNotifications());
}

@ReactMethod
/**
* Removes notifications from the Notification Center, whose id matches
* an element in the provided array
*/
public void removeDeliveredNotifications(ReadableArray identifiers) {
mRNPushNotificationHelper.clearDeliveredNotifications(identifiers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import androidx.core.app.NotificationCompat;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -527,6 +532,40 @@ public void clearNotification(int notificationID) {
notificationManager.cancel(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));
}
}

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;

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

Expand Down
11 changes: 11 additions & 0 deletions component/index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ NotificationsComponent.prototype.clearAllNotifications = function() {
RNPushNotification.clearAllNotifications()
}

NotificationsComponent.prototype.removeAllDeliveredNotifications = function() {
RNPushNotification.removeAllDeliveredNotifications();
}

NotificationsComponent.prototype.getDeliveredNotifications = function(callback: Function) {
RNPushNotification.getDeliveredNotifications(callback);
}
NotificationsComponent.prototype.removeDeliveredNotifications = function(identifiers: Array) {
RNPushNotification.removeDeliveredNotifications(identifiers);
}

module.exports = {
state: false,
component: new NotificationsComponent()
Expand Down
12 changes: 12 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,16 @@ Notifications.clearAllNotifications = function() {
return this.callNative('clearAllNotifications', arguments)
}

Notifications.removeAllDeliveredNotifications = function() {
return this.callNative('removeAllDeliveredNotifications', arguments);
}

Notifications.getDeliveredNotifications = function() {
return this.callNative('getDeliveredNotifications', arguments);
}

Notifications.removeDeliveredNotifications = function() {
return this.callNative('removeDeliveredNotifications', arguments);
}

module.exports = Notifications;