From 7629b5ad38a43165da6a8f25716022ad91640643 Mon Sep 17 00:00:00 2001 From: kreativ-software Date: Tue, 3 Mar 2020 16:07:16 +0100 Subject: [PATCH] [fix] no notification in tray when app in foreground (like firebase) --- .../notificationhub/NotificationHubUtil.java | 10 ++++++++++ .../ReactNativeFirebaseMessagingService.java | 9 ++++++++- .../ReactNativeNotificationHubModule.java | 18 ++++++++++++++---- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/azure/reactnative/notificationhub/NotificationHubUtil.java b/android/src/main/java/com/azure/reactnative/notificationhub/NotificationHubUtil.java index f271c807..9b4fc397 100644 --- a/android/src/main/java/com/azure/reactnative/notificationhub/NotificationHubUtil.java +++ b/android/src/main/java/com/azure/reactnative/notificationhub/NotificationHubUtil.java @@ -25,6 +25,8 @@ public class NotificationHubUtil { private static final String KEY_FOR_PREFS_CHANNELENABLELIGHTS = "AzureNotificationHub_channelEnableLights"; private static final String KEY_FOR_PREFS_CHANNELENABLEVIBRATION = "AzureNotificationHub_channelEnableVibration"; + private boolean mIsForeground; + public static NotificationHubUtil getInstance() { if (sharedNotificationHubUtilInstance == null) { sharedNotificationHubUtilInstance = new NotificationHubUtil(); @@ -130,6 +132,14 @@ public boolean hasChannelEnableVibration(Context context) { return hasKey(context, KEY_FOR_PREFS_CHANNELENABLEVIBRATION); } + public void setAppIsForeground(boolean isForeground) { + mIsForeground = isForeground; + } + + public boolean getAppIsForeground() { + return mIsForeground; + } + public NotificationHub createNotificationHub(String hubName, String connectionString, ReactContext reactContext) { NotificationHub hub = new NotificationHub(hubName, connectionString, reactContext); return hub; diff --git a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java index c7271953..85b054e3 100644 --- a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java +++ b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeFirebaseMessagingService.java @@ -58,6 +58,7 @@ public void onNewToken(String token) { @Override public void onMessageReceived(RemoteMessage remoteMessage) { + NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance(); Log.d(TAG, "Remote message from: " + remoteMessage.getFrom()); if (notificationChannelID == null) { @@ -65,7 +66,13 @@ public void onMessageReceived(RemoteMessage remoteMessage) { } Bundle bundle = remoteMessage.toIntent().getExtras(); - ReactNativeNotificationsHandler.sendNotification(this, bundle, notificationChannelID); + if (notificationHubUtil.getAppIsForeground()) { + bundle.putBoolean("foreground", true); + bundle.putBoolean("userInteraction", false); + bundle.putBoolean("coldstart", false); + } else { + ReactNativeNotificationsHandler.sendNotification(this, bundle, notificationChannelID); + } ReactNativeNotificationsHandler.sendBroadcast(this, bundle, 0); } } diff --git a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java index 578082e4..cdeea7d2 100644 --- a/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java +++ b/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java @@ -49,7 +49,6 @@ public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule private ReactApplicationContext mReactContext; private LocalBroadcastReceiver mLocalBroadcastReceiver; - private boolean mIsForeground; public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) { super(reactContext); @@ -67,6 +66,16 @@ public String getName() { return AZURE_NOTIFICATION_HUB_NAME; } + public void setIsForeground(boolean isForeground) { + NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance(); + notificationHubUtil.setAppIsForeground(isForeground); + } + + public boolean getIsForeground() { + NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance(); + return notificationHubUtil.getAppIsForeground(); + } + @ReactMethod public void register(ReadableMap config, Promise promise) { NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance(); @@ -168,7 +177,8 @@ public void unregister(Promise promise) { @Override public void onHostResume() { - mIsForeground = true; + setIsForeground(true); + Activity activity = getCurrentActivity(); if (activity != null) { Intent intent = activity.getIntent(); @@ -188,7 +198,7 @@ public void onHostResume() { @Override public void onHostPause() { - mIsForeground = false; + setIsForeground(false); } @Override @@ -213,7 +223,7 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode, public class LocalBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - if (mIsForeground) { + if (getIsForeground()) { String event = intent.getStringExtra("event"); String data = intent.getStringExtra("data"); mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)