Skip to content

Commit faa5c21

Browse files
authored
Merge pull request #51 from CatalystCode/android-pn-inactive
Handling clicking on notification
2 parents ac54b96 + 6ab97c4 commit faa5c21

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
@@ -42,21 +42,32 @@ public class ReactNativeNotificationsHandler extends NotificationsHandler {
4242
public void onReceive(Context context, Bundle bundle) {
4343
this.context = context;
4444
sendNotification(bundle);
45+
sendBroadcast(context, bundle, 0);
46+
}
4547

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

55-
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
5662
Intent event= new Intent(TAG);
5763
event.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
5864
event.putExtra("data", json.toString());
65+
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
5966
localBroadcastManager.sendBroadcast(event);
67+
}
68+
catch (Exception e) {}
69+
}
70+
}).start();
6071
}
6172

6273
private Class getMainActivityClass() {

0 commit comments

Comments
 (0)