Skip to content

Commit 1c7db45

Browse files
authored
Merge pull request #7 from rozele/issue3
feat(Unregister): Add unregistration feature to Android
2 parents f26bc67 + d80b629 commit 1c7db45

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

android/react-native-azurenotificationhub.iml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@
6262
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
6363
<sourceFolder url="file://$MODULE_DIR$/src/main/jni" isTestSource="false" />
6464
<sourceFolder url="file://$MODULE_DIR$/src/main/rs" isTestSource="false" />
65-
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
66-
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
67-
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
68-
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
69-
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
70-
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
71-
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
7265
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
7366
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
7467
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
7568
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
7669
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
7770
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
7871
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
72+
<sourceFolder url="file://$MODULE_DIR$/src/test/res" type="java-test-resource" />
73+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
74+
<sourceFolder url="file://$MODULE_DIR$/src/test/assets" type="java-test-resource" />
75+
<sourceFolder url="file://$MODULE_DIR$/src/test/aidl" isTestSource="true" />
76+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
77+
<sourceFolder url="file://$MODULE_DIR$/src/test/jni" isTestSource="true" />
78+
<sourceFolder url="file://$MODULE_DIR$/src/test/rs" isTestSource="true" />
7979
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" />
8080
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
8181
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
@@ -97,20 +97,20 @@
9797
<orderEntry type="library" exported="" name="bolts-tasks-1.4.0" level="project" />
9898
<orderEntry type="library" exported="" name="recyclerview-v7-23.0.1" level="project" />
9999
<orderEntry type="library" exported="" name="okhttp-urlconnection-3.4.1" level="project" />
100+
<orderEntry type="library" exported="" name="react-native-0.33.0" level="project" />
100101
<orderEntry type="library" exported="" name="android-jsc-r174650" level="project" />
101102
<orderEntry type="library" exported="" name="support-v4-23.2.1" level="project" />
102103
<orderEntry type="library" exported="" name="firebase-common-9.4.0" level="project" />
103104
<orderEntry type="library" exported="" name="firebase-iid-9.4.0" level="project" />
104105
<orderEntry type="library" exported="" name="support-annotations-23.2.1" level="project" />
105-
<orderEntry type="library" exported="" name="appcompat-v7-23.0.1" level="project" />
106106
<orderEntry type="library" exported="" name="fbcore-0.11.0" level="project" />
107+
<orderEntry type="library" exported="" name="appcompat-v7-23.0.1" level="project" />
107108
<orderEntry type="library" exported="" name="imagepipeline-0.11.0" level="project" />
108109
<orderEntry type="library" exported="" name="play-services-gcm-9.4.0" level="project" />
109110
<orderEntry type="library" exported="" name="firebase-messaging-9.4.0" level="project" />
110111
<orderEntry type="library" exported="" name="library-2.4.0" level="project" />
111112
<orderEntry type="library" exported="" name="imagepipeline-base-0.11.0" level="project" />
112113
<orderEntry type="library" exported="" name="play-services-iid-9.4.0" level="project" />
113-
<orderEntry type="library" exported="" name="react-native-0.34.0-rc.0" level="project" />
114114
<orderEntry type="library" exported="" name="soloader-0.1.0" level="project" />
115115
<orderEntry type="library" exported="" name="play-services-basement-9.4.0" level="project" />
116116
<orderEntry type="library" exported="" name="javax.inject-1" level="project" />
@@ -122,4 +122,4 @@
122122
<orderEntry type="library" exported="" name="imagepipeline-okhttp3-0.11.0" level="project" />
123123
<orderEntry type="library" exported="" name="azure-notifications-handler-1.0.1" level="project" />
124124
</component>
125-
</module>
125+
</module>

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55

66
import com.google.android.gms.common.ConnectionResult;
77
import com.google.android.gms.common.GoogleApiAvailability;
8-
import com.google.android.gms.gcm.*;
98

9+
import com.microsoft.windowsazure.messaging.NotificationHub;
1010
import com.microsoft.windowsazure.notifications.NotificationsManager;
1111

12-
import com.facebook.react.bridge.ActivityEventListener;
1312
import com.facebook.react.bridge.Promise;
1413
import com.facebook.react.bridge.ReactApplicationContext;
1514
import com.facebook.react.bridge.ReactContext;
@@ -23,6 +22,8 @@ public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule
2322

2423
private static final String ERROR_INVALID_ARGUMENTS = "E_INVALID_ARGUMENTS";
2524
private static final String ERROR_PLAY_SERVICES = "E_PLAY_SERVICES";
25+
private static final String ERROR_NOTIFICATION_HUB = "E_NOTIFICATION_HUB";
26+
private static final String ERROR_NOT_REGISTERED = "E_NOT_REGISTERED";
2627

2728
public ReactNativeNotificationHubModule(ReactApplicationContext reactContext) {
2829
super(reactContext);
@@ -77,8 +78,26 @@ public void register(ReadableMap config, Promise promise) {
7778
}
7879

7980
@ReactMethod
80-
public void unregister(ReadableMap config) {
81+
public void unregister(Promise promise) {
82+
NotificationHubUtil notificationHubUtil = NotificationHubUtil.getInstance();
83+
84+
ReactContext reactContext = getReactApplicationContext();
85+
String connectionString = notificationHubUtil.getConnectionString(reactContext);
86+
String hubName = notificationHubUtil.getHubName(reactContext);
87+
String registrationId = notificationHubUtil.getRegistrationID(reactContext);
88+
89+
if (connectionString == null || hubName == null || registrationId == null) {
90+
promise.reject(ERROR_NOT_REGISTERED, "No registration to Azure Notification Hub.");
91+
}
92+
93+
NotificationHub hub = new NotificationHub(hubName, connectionString, reactContext);
94+
try {
95+
hub.unregister();
96+
} catch (Exception e) {
97+
promise.reject(ERROR_NOTIFICATION_HUB, e);
98+
}
8199

100+
NotificationsManager.stopHandlingNotifications(reactContext);
82101
}
83102

84103
private static class GoogleApiAvailabilityRunnable implements Runnable {

0 commit comments

Comments
 (0)