9
9
10
10
import androidx .localbroadcastmanager .content .LocalBroadcastManager ;
11
11
12
+ import com .facebook .react .bridge .ActivityEventListener ;
12
13
import com .facebook .react .bridge .LifecycleEventListener ;
13
14
import com .facebook .react .modules .core .DeviceEventManagerModule ;
14
15
import com .google .android .gms .common .ConnectionResult ;
25
26
import com .facebook .react .bridge .ReadableMap ;
26
27
import com .facebook .react .bridge .UiThreadUtil ;
27
28
28
- public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
29
+ public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements
30
+ ActivityEventListener , LifecycleEventListener {
29
31
public static final String AZURE_NOTIFICATION_HUB_NAME = "AzureNotificationHub" ;
30
32
public static final String NOTIF_REGISTER_AZURE_HUB_EVENT = "azureNotificationHubRegistered" ;
31
33
public static final String NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT = "azureNotificationHubRegistrationError" ;
@@ -47,6 +49,7 @@ public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule
47
49
48
50
private ReactApplicationContext mReactContext ;
49
51
private LocalBroadcastReceiver mLocalBroadcastReceiver ;
52
+ private boolean mIsForeground ;
50
53
51
54
public ReactNativeNotificationHubModule (ReactApplicationContext reactContext ) {
52
55
super (reactContext );
@@ -56,6 +59,7 @@ public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
56
59
localBroadcastManager .registerReceiver (mLocalBroadcastReceiver , new IntentFilter (ReactNativeRegistrationIntentService .TAG ));
57
60
localBroadcastManager .registerReceiver (mLocalBroadcastReceiver , new IntentFilter (ReactNativeNotificationsHandler .TAG ));
58
61
reactContext .addLifecycleEventListener (this );
62
+ reactContext .addActivityEventListener (this );
59
63
}
60
64
61
65
@ Override
@@ -164,12 +168,16 @@ public void unregister(Promise promise) {
164
168
165
169
@ Override
166
170
public void onHostResume () {
171
+ mIsForeground = true ;
167
172
Activity activity = getCurrentActivity ();
168
173
if (activity != null ) {
169
174
Intent intent = activity .getIntent ();
170
175
if (intent != null ) {
171
176
Bundle bundle = intent .getBundleExtra ("notification" );
172
177
if (bundle != null ) {
178
+ bundle .putBoolean ("foreground" , false );
179
+ bundle .putBoolean ("userInteraction" , true );
180
+ bundle .putBoolean ("coldstart" , true );
173
181
ReactNativeNotificationsHandler .sendBroadcast (
174
182
mReactContext , bundle , NOTIFICATION_DELAY_ON_START );
175
183
}
@@ -179,19 +187,37 @@ public void onHostResume() {
179
187
180
188
@ Override
181
189
public void onHostPause () {
190
+ mIsForeground = false ;
182
191
}
183
192
184
193
@ Override
185
194
public void onHostDestroy () {
186
195
}
187
196
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
+
188
212
public class LocalBroadcastReceiver extends BroadcastReceiver {
189
213
@ Override
190
214
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
+ }
195
221
}
196
222
}
197
223
0 commit comments