CatalystCode / react-native-azurenotificationhub

React Native module to support Azure Notification Hub push notifications on Android, iOS, and Windows.
MIT License
47 stars 75 forks source link

Notify apps about device token #151

Open phongcao opened 4 years ago

phongcao commented 4 years ago

Similar to what iOS currently has, Android should provide a way to notify apps about device token.

DiogoAbu commented 4 years ago

I`m using patch-package and wrote this, if helps anyone.

react-native-azurenotificationhub/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeConstants.java

@@ -124,6 +124,8 @@ public final class ReactNativeConstants {
     public static final String ERROR_ACTIVITY_INTENT_IS_NULL = "Activity intent is null";
     public static final String ERROR_GET_UUID = "E_GET_UUID";
     public static final String ERROR_NO_UUID_SET = "No uuid set";
+    public static final String ERROR_GET_FCM_TOKEN = "E_GET_FCM_TOKEN";
+    public static final String ERROR_NO_FCM_TOKEN_SET = "The FCM token is only available after registration.";

     private ReactNativeConstants() {
     }

react-native-azurenotificationhub/android/src/main/java/com/azure/reactnative/notificationhub/ReactNativeNotificationHubModule.java

@@ -364,6 +364,19 @@ public class ReactNativeNotificationHubModule extends ReactContextBaseJavaModule
         }
     }

+    @ReactMethod
+    public void getFCMToken(Promise promise) {
+        ReactNativeNotificationHubUtil notificationHubUtil = ReactNativeNotificationHubUtil.getInstance();
+        ReactContext reactContext = getReactApplicationContext();
+        String FCMToken = notificationHubUtil.getFCMToken(reactContext);
+
+        if (FCMToken != null) {
+            promise.resolve(FCMToken);
+        } else {
+            promise.reject(ERROR_GET_FCM_TOKEN, ERROR_NO_FCM_TOKEN_SET);
+        }
+    }
+
     @ReactMethod
     public void isNotificationEnabledOnOSLevel(Promise promise) {
         ReactContext reactContext = getReactApplicationContext();