99
1010import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
1111
12+ import com .facebook .react .bridge .ActivityEventListener ;
1213import com .facebook .react .bridge .LifecycleEventListener ;
1314import com .facebook .react .modules .core .DeviceEventManagerModule ;
1415import com .google .android .gms .common .ConnectionResult ;
2526import com .facebook .react .bridge .ReadableMap ;
2627import 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
0 commit comments