Skip to content

Commit 6ab97c4

Browse files
committed
Handled push notification after the app has been closed (user taps on notification).
1 parent b0e5ca2 commit 6ab97c4

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,14 @@ const senderID = '...'; // The Sender ID from the Cloud Messaging tab of
473473
const tags = [ ... ]; // The set of tags to subscribe to
474474

475475
class myapp extends Component {
476-
register()
476+
constructor(props) {
477+
super(props);
478+
PushNotificationEmitter.addListener(DEVICE_NOTIF_EVENT, this._onRemoteNotification);
479+
}
480+
481+
register() {
477482
PushNotificationEmitter.addListener(NOTIF_REGISTER_AZURE_HUB_EVENT, this._onAzureNotificationHubRegistered);
478483
PushNotificationEmitter.addListener(NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT, this._onAzureNotificationHubRegistrationError);
479-
PushNotificationEmitter.addListener(DEVICE_NOTIF_EVENT, this._onRemoteNotification);
480484

481485
NotificationHub.register({connectionString, hubName, senderID, tags})
482486
.catch(reason => console.warn(reason));

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import android.content.Context;
66
import android.content.Intent;
77
import android.content.IntentFilter;
8+
import android.os.Bundle;
89
import android.support.v4.content.LocalBroadcastManager;
910

11+
import com.facebook.react.bridge.LifecycleEventListener;
1012
import com.facebook.react.modules.core.DeviceEventManagerModule;
1113
import com.google.android.gms.common.ConnectionResult;
1214
import com.google.android.gms.common.GoogleApiAvailability;
@@ -23,12 +25,13 @@
2325
import com.facebook.react.bridge.ReadableMap;
2426
import com.facebook.react.bridge.UiThreadUtil;
2527

26-
public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule {
28+
public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
2729
public static final String NOTIF_REGISTER_AZURE_HUB_EVENT = "azureNotificationHubRegistered";
2830
public static final String NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT = "azureNotificationHubRegistrationError";
2931
public static final String DEVICE_NOTIF_EVENT = "remoteNotificationReceived";
3032

3133
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
34+
private static final int NOTIFICATION_DELAY_ON_START = 3000;
3235

3336
private static final String ERROR_INVALID_ARGUMENTS = "E_INVALID_ARGUMENTS";
3437
private static final String ERROR_PLAY_SERVICES = "E_PLAY_SERVICES";
@@ -45,6 +48,7 @@ public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
4548
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(reactContext);
4649
localBroadcastManager.registerReceiver(mLocalBroadcastReceiver, new IntentFilter(ReactNativeRegistrationIntentService.TAG));
4750
localBroadcastManager.registerReceiver(mLocalBroadcastReceiver, new IntentFilter(ReactNativeNotificationsHandler.TAG));
51+
reactContext.addLifecycleEventListener(this);
4852
}
4953

5054
@Override
@@ -128,6 +132,23 @@ public void unregister(Promise promise) {
128132
}
129133
}
130134

135+
@Override
136+
public void onHostResume() {
137+
Activity activity = getCurrentActivity();
138+
if (activity != null) {
139+
Intent intent = activity.getIntent();
140+
if (intent != null) {
141+
Bundle bundle = intent.getBundleExtra("notification");
142+
if (bundle != null) {
143+
new ReactNativeNotificationsHandler().sendBroadcast(mReactContext, bundle, NOTIFICATION_DELAY_ON_START);
144+
}
145+
}
146+
}
147+
}
148+
@Override
149+
public void onHostPause() {}
150+
@Override
151+
public void onHostDestroy() {}
131152
public class LocalBroadcastReceiver extends BroadcastReceiver {
132153
@Override
133154
public void onReceive(Context context, Intent intent) {

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,32 @@ public class ReactNativeNotificationsHandler extends NotificationsHandler {
4141
public void onReceive(Context context, Bundle bundle) {
4242
this.context = context;
4343
sendNotification(bundle);
44+
sendBroadcast(context, bundle, 0);
45+
}
4446

47+
public void sendBroadcast(final Context context, final Bundle bundle, final long delay) {
48+
(new Thread() {
49+
public void run() {
50+
try
51+
{
52+
Thread.currentThread().sleep(delay);
4553
JSONObject json = new JSONObject();
4654
Set<String> keys = bundle.keySet();
4755
for (String key : keys) {
4856
try {
4957
json.put(key, bundle.get(key));
50-
} catch (JSONException e) {
58+
} catch (JSONException e) {}
5159
}
52-
}
5360

54-
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
5561
Intent event= new Intent(TAG);
5662
event.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
5763
event.putExtra("data", json.toString());
64+
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
5865
localBroadcastManager.sendBroadcast(event);
66+
}
67+
catch (Exception e) {}
68+
}
69+
}).start();
5970
}
6071

6172
private Class getMainActivityClass() {

0 commit comments

Comments
 (0)