Skip to content

Commit 1f3e998

Browse files
swabbassyogevbd
andauthored
notifyReceivedToJS only when app is visible (#694)
When app in the background the js thread is not granted to be running to making sure it is only sent when the app is in FG. Co-authored-by: yogevbd <[email protected]>
1 parent 31a3502 commit 1f3e998

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/android/app/src/main/java/com/wix/reactnativenotifications/Defs.java

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ public interface Defs {
66
String TOKEN_RECEIVED_EVENT_NAME = "remoteNotificationsRegistered";
77

88
String NOTIFICATION_RECEIVED_EVENT_NAME = "notificationReceived";
9+
String NOTIFICATION_RECEIVED_BACKGROUND_EVENT_NAME = "notificationReceivedBackground";
910
String NOTIFICATION_OPENED_EVENT_NAME = "notificationOpened";
1011
}

lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_OPENED_EVENT_NAME;
2323
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_EVENT_NAME;
24+
import static com.wix.reactnativenotifications.Defs.NOTIFICATION_RECEIVED_BACKGROUND_EVENT_NAME;
2425

2526
public class PushNotification implements IPushNotification {
2627

@@ -61,8 +62,10 @@ protected PushNotification(Context context, Bundle bundle, AppLifecycleFacade ap
6162
public void onReceived() throws InvalidNotificationException {
6263
if (!mAppLifecycleFacade.isAppVisible()) {
6364
postNotification(null);
65+
notifyReceivedBackgroundToJS();
66+
} else {
67+
notifyReceivedToJS();
6468
}
65-
notifyReceivedToJS();
6669
}
6770

6871
@Override
@@ -204,6 +207,10 @@ private void notifyReceivedToJS() {
204207
mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext());
205208
}
206209

210+
private void notifyReceivedBackgroundToJS() {
211+
mJsIOHelper.sendEventToJS(NOTIFICATION_RECEIVED_BACKGROUND_EVENT_NAME, mNotificationProps.asBundle(), mAppLifecycleFacade.getRunningReactContext());
212+
}
213+
207214
private void notifyOpenedToJS() {
208215
Bundle response = new Bundle();
209216
response.putBundle("notification", mNotificationProps.asBundle());

0 commit comments

Comments
 (0)