diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java index d16268059..469f565b1 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java @@ -163,6 +163,20 @@ public void unsubscribeFromTopic(String topic) { FirebaseMessaging.getInstance().unsubscribeFromTopic(topic); } + + @ReactMethod + public void getFCMToken(final Promise promise) { + FirebaseMessaging.getInstance().getToken() + .addOnCompleteListener(task -> { + if (!task.isSuccessful()) { + Log.e(LOG_TAG, "exception", task.getException()); + promise.reject(task.getException()); + } else { + promise.resolve(task.getResult()); + } + }); + } + @ReactMethod public void presentLocalNotification(ReadableMap details) { Bundle bundle = Arguments.toBundle(details); diff --git a/component/index.android.js b/component/index.android.js index 0a609cbf9..ea880f912 100644 --- a/component/index.android.js +++ b/component/index.android.js @@ -36,6 +36,18 @@ NotificationsComponent.prototype.unsubscribeFromTopic = function(topic) { RNPushNotification.unsubscribeFromTopic(topic); }; +NotificationsComponent.prototype.getFCMToken = function() { + return new Promise(function(resolve, reject) { + RNPushNotification.getFCMToken() + .then(function(token) { + resolve(token); + }) + .catch(function(err) { + reject(err); + }); + }); +}; + NotificationsComponent.prototype.cancelLocalNotification = function(details) { RNPushNotification.cancelLocalNotification(details); }; diff --git a/index.js b/index.js index 716ea0ed2..d9b78c3de 100644 --- a/index.js +++ b/index.js @@ -475,6 +475,10 @@ Notifications.unsubscribeFromTopic = function () { return this.callNative('unsubscribeFromTopic', arguments); }; +Notifications.getFCMToken = function () { + return this.callNative('getFCMToken', arguments); +}; + Notifications.presentLocalNotification = function() { return this.callNative('presentLocalNotification', arguments); };