Skip to content

Commit d1d1f1c

Browse files
committed
Added listeners for registration and remote notification.
1 parent ec7dcc0 commit d1d1f1c

File tree

3 files changed

+65
-20
lines changed

3 files changed

+65
-20
lines changed

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.azure.reactnative.notificationhub;
22

33
import android.app.Activity;
4+
import android.content.BroadcastReceiver;
5+
import android.content.Context;
46
import android.content.Intent;
7+
import android.content.IntentFilter;
8+
import android.support.v4.content.LocalBroadcastManager;
59

10+
import com.facebook.react.modules.core.DeviceEventManagerModule;
611
import com.google.android.gms.common.ConnectionResult;
712
import com.google.android.gms.common.GoogleApiAvailability;
813

@@ -19,15 +24,27 @@
1924
import com.facebook.react.bridge.UiThreadUtil;
2025

2126
public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule {
27+
public static final String NOTIF_REGISTER_AZURE_HUB_EVENT = "azureNotificationHubRegistered";
28+
public static final String NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT = "azureNotificationHubRegistrationError";
29+
public static final String DEVICE_NOTIF_EVENT = "remoteNotificationReceived";
30+
2231
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
2332

2433
private static final String ERROR_INVALID_ARGUMENTS = "E_INVALID_ARGUMENTS";
2534
private static final String ERROR_PLAY_SERVICES = "E_PLAY_SERVICES";
2635
private static final String ERROR_NOTIFICATION_HUB = "E_NOTIFICATION_HUB";
2736
private static final String ERROR_NOT_REGISTERED = "E_NOT_REGISTERED";
2837

38+
private ReactApplicationContext mReactContext;
39+
private LocalBroadcastReceiver mLocalBroadcastReceiver;
40+
2941
public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
3042
super(reactContext);
43+
this.mReactContext = reactContext;
44+
this.mLocalBroadcastReceiver = new LocalBroadcastReceiver();
45+
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(reactContext);
46+
localBroadcastManager.registerReceiver(mLocalBroadcastReceiver, new IntentFilter(ReactNativeRegistrationIntentService.TAG));
47+
localBroadcastManager.registerReceiver(mLocalBroadcastReceiver, new IntentFilter(ReactNativeNotificationsHandler.TAG));
3148
}
3249

3350
@Override
@@ -110,20 +127,16 @@ public void unregister(Promise promise) {
110127
promise.reject(ERROR_NOTIFICATION_HUB, e);
111128
}
112129
}
113-
114-
@ReactMethod
115-
public void getRegistrationId(Promise promise) {
116-
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
117-
ReactContext reactContext = getReactApplicationContext();
118-
String registrationId = notificationHubUtil.getRegistrationID(reactContext);
119130

120-
if (registrationId == null) {
121-
promise.reject(ERROR_NOT_REGISTERED, "No registration to Azure Notification Hub.");
122-
}
123-
else {
124-
promise.resolve(registrationId);
131+
public class LocalBroadcastReceiver extends BroadcastReceiver {
132+
@Override
133+
public void onReceive(Context context, Intent intent) {
134+
String event = intent.getStringExtra("event");
135+
String data = intent.getStringExtra("data");
136+
mReactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
137+
.emit(event, data);
125138
}
126-
}
139+
}
127140

128141
private static class GoogleApiAvailabilityRunnable implements Runnable {
129142
private final Activity activity;

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@
1717
import android.os.Build;
1818
import android.os.Bundle;
1919
import android.support.v4.app.NotificationCompat;
20+
import android.support.v4.content.LocalBroadcastManager;
2021
import android.util.Log;
2122

2223
import com.facebook.react.bridge.ReadableMap;
2324

2425
import org.json.JSONArray;
2526
import org.json.JSONException;
27+
import org.json.JSONObject;
2628

2729
import com.microsoft.windowsazure.notifications.NotificationsHandler;
2830

31+
import java.util.Set;
32+
2933
public class ReactNativeNotificationsHandler extends NotificationsHandler {
30-
private static final String LOG_TAG = "AzureNotificationHub";
34+
public static final String TAG = "ReactNativeNotificationsHandler";
35+
3136
private static final long DEFAULT_VIBRATION = 300L;
3237

3338
private Context context;
@@ -36,6 +41,21 @@ public class ReactNativeNotificationsHandler extends NotificationsHandler {
3641
public void onReceive(Context context, Bundle bundle) {
3742
this.context = context;
3843
sendNotification(bundle);
44+
45+
JSONObject json = new JSONObject();
46+
Set<String> keys = bundle.keySet();
47+
for (String key : keys) {
48+
try {
49+
json.put(key, bundle.get(key));
50+
} catch (JSONException e) {
51+
}
52+
}
53+
54+
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);
55+
Intent event= new Intent(TAG);
56+
event.putExtra("event", ReactNativeNotificationHubModule.DEVICE_NOTIF_EVENT);
57+
event.putExtra("data", json.toString());
58+
localBroadcastManager.sendBroadcast(event);
3959
}
4060

4161
private Class getMainActivityClass() {
@@ -58,18 +78,18 @@ private void sendNotification(Bundle bundle) {
5878
try {
5979
Class intentClass = getMainActivityClass();
6080
if (intentClass == null) {
61-
Log.e(LOG_TAG, "No activity class found for the notification");
81+
Log.e(TAG, "No activity class found for the notification");
6282
return;
6383
}
6484

6585
if (bundle.getString("message") == null) {
66-
Log.e(LOG_TAG, "No message specified for the notification");
86+
Log.e(TAG, "No message specified for the notification");
6787
return;
6888
}
6989

7090
String notificationIdString = bundle.getString("id");
7191
if (notificationIdString == null) {
72-
Log.e(LOG_TAG, "No notification ID specified for the notification");
92+
Log.e(TAG, "No notification ID specified for the notification");
7393
return;
7494
}
7595

@@ -211,7 +231,7 @@ private void sendNotification(Bundle bundle) {
211231
try {
212232
actionsArray = bundle.getString("actions") != null ? new JSONArray(bundle.getString("actions")) : null;
213233
} catch (JSONException e) {
214-
Log.e(LOG_TAG, "Exception while converting actions to JSON object.", e);
234+
Log.e(TAG, "Exception while converting actions to JSON object.", e);
215235
}
216236

217237
if (actionsArray != null) {
@@ -224,7 +244,7 @@ private void sendNotification(Bundle bundle) {
224244
try {
225245
action = actionsArray.getString(i);
226246
} catch (JSONException e) {
227-
Log.e(LOG_TAG, "Exception while getting action from actionsArray.", e);
247+
Log.e(TAG, "Exception while getting action from actionsArray.", e);
228248
continue;
229249
}
230250

@@ -249,7 +269,7 @@ private void sendNotification(Bundle bundle) {
249269
notificationManager.notify(notificationID, info);
250270
}
251271
} catch (Exception e) {
252-
Log.e(LOG_TAG, "failed to send push notification", e);
272+
Log.e(TAG, "failed to send push notification", e);
253273
}
254274
}
255275

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.IntentService;
44
import android.content.Intent;
5+
import android.support.v4.content.LocalBroadcastManager;
56
import android.util.Log;
67

78
import com.google.firebase.FirebaseApp;
@@ -11,14 +12,17 @@
1112

1213
public class ReactNativeRegistrationIntentService extends IntentService {
1314

14-
private static final String TAG = "AzureNotificationHub";
15+
public static final String TAG = "ReactNativeRegistration";
1516

1617
public ReactNativeRegistrationIntentService() {
1718
super(TAG);
1819
}
1920

2021
@Override
2122
protected void onHandleIntent(Intent intent) {
23+
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
24+
Intent event= new Intent(TAG);
25+
2226
try {
2327
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
2428
String connectionString = notificationHubUtil.getConnectionString(this);
@@ -49,9 +53,17 @@ protected void onHandleIntent(Intent intent) {
4953

5054
notificationHubUtil.setRegistrationID(this, regID);
5155
notificationHubUtil.setFCMToken(this, token);
56+
57+
event.putExtra("event", ReactNativeNotificationHubModule.NOTIF_REGISTER_AZURE_HUB_EVENT);
58+
event.putExtra("data", regID);
59+
localBroadcastManager.sendBroadcast(event);
5260
}
5361
} catch (Exception e) {
5462
Log.e(TAG, "Failed to complete token refresh", e);
63+
64+
event.putExtra("event", ReactNativeNotificationHubModule.NOTIF_AZURE_HUB_REGISTRATION_ERROR_EVENT);
65+
event.putExtra("data", e.getMessage());
66+
localBroadcastManager.sendBroadcast(event);
5567
}
5668
}
5769
}

0 commit comments

Comments
 (0)