Skip to content

Commit 687f915

Browse files
committed
Supported 'foreground', 'userInteraction' and 'coldstart' when delivering notificaation.
1 parent 2f06e40 commit 687f915

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
1111

12+
import com.facebook.react.bridge.ActivityEventListener;
1213
import com.facebook.react.bridge.LifecycleEventListener;
1314
import com.facebook.react.modules.core.DeviceEventManagerModule;
1415
import com.google.android.gms.common.ConnectionResult;
@@ -25,7 +26,8 @@
2526
import com.facebook.react.bridge.ReadableMap;
2627
import com.facebook.react.bridge.UiThreadUtil;
2728

28-
public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
29+
public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements
30+
ActivityEventListener, LifecycleEventListener {
2931
public static final String AZURE_NOTIFICATION_HUB_NAME = "AzureNotificationHub";
3032
public static final String NOTIF_REGISTER_AZURE_HUB_EVENT = "azureNotificationHubRegistered";
3133
public static final String NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT = "azureNotificationHubRegistrationError";
@@ -47,6 +49,7 @@ public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule
4749

4850
private ReactApplicationContext mReactContext;
4951
private LocalBroadcastReceiver mLocalBroadcastReceiver;
52+
private boolean mIsForeground;
5053

5154
public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
5255
super(reactContext);
@@ -56,6 +59,7 @@ public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
5659
localBroadcastManager.registerReceiver(mLocalBroadcastReceiver, new IntentFilter(ReactNativeRegistrationIntentService.TAG));
5760
localBroadcastManager.registerReceiver(mLocalBroadcastReceiver, new IntentFilter(ReactNativeNotificationsHandler.TAG));
5861
reactContext.addLifecycleEventListener(this);
62+
reactContext.addActivityEventListener(this);
5963
}
6064

6165
@Override
@@ -164,12 +168,16 @@ public void unregister(Promise promise) {
164168

165169
@Override
166170
public void onHostResume() {
171+
mIsForeground = true;
167172
Activity activity = getCurrentActivity();
168173
if (activity != null) {
169174
Intent intent = activity.getIntent();
170175
if (intent != null) {
171176
Bundle bundle = intent.getBundleExtra("notification");
172177
if (bundle != null) {
178+
bundle.putBoolean("foreground", false);
179+
bundle.putBoolean("userInteraction", true);
180+
bundle.putBoolean("coldstart", true);
173181
ReactNativeNotificationsHandler.sendBroadcast(
174182
mReactContext, bundle, NOTIFICATION_DELAY_ON_START);
175183
}
@@ -179,19 +187,37 @@ public void onHostResume() {
179187

180188
@Override
181189
public void onHostPause() {
190+
mIsForeground = false;
182191
}
183192

184193
@Override
185194
public void onHostDestroy() {
186195
}
187196

197+
@Override
198+
public void onNewIntent(Intent intent) {
199+
Bundle bundle = intent.getBundleExtra("notification");
200+
if (bundle != null) {
201+
bundle.putBoolean("foreground", false);
202+
bundle.putBoolean("userInteraction", true);
203+
ReactNativeNotificationsHandler.sendBroadcast(
204+
mReactContext, bundle, NOTIFICATION_DELAY_ON_START);
205+
}
206+
}
207+
208+
@Override
209+
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
210+
}
211+
188212
public class LocalBroadcastReceiver extends BroadcastReceiver {
189213
@Override
190214
public void onReceive(Context context, Intent intent) {
191-
String event = intent.getStringExtra("event");
192-
String data = intent.getStringExtra("data");
193-
mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
194-
.emit(event, data);
215+
if (mIsForeground) {
216+
String event = intent.getStringExtra("event");
217+
String data = intent.getStringExtra("data");
218+
mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
219+
.emit(event, data);
220+
}
195221
}
196222
}
197223

android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationsHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ public static void sendNotification(Context context, Bundle bundle, String notif
180180

181181
Intent intent = new Intent(context, intentClass);
182182
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
183-
bundle.putBoolean("userInteraction", true);
183+
bundle.putBoolean("foreground", true);
184+
bundle.putBoolean("userInteraction", false);
185+
bundle.putBoolean("coldstart", false);
184186
intent.putExtra("notification", bundle);
185187

186188
if (!bundle.containsKey("playSound") || bundle.getBoolean("playSound")) {

0 commit comments

Comments
 (0)