From fd8f4a4c50376d640179b62363840d3bf9c9620f Mon Sep 17 00:00:00 2001 From: Libin Lu Date: Thu, 9 Jun 2016 13:37:22 -0400 Subject: [PATCH] add subscribe to topic feature and fix event typo --- android/react-native-fcm.iml | 21 ++++++----- .../evollu/react/fcm/FIRMessagingModule.java | 11 ++++++ index.js | 37 ++++++++++--------- ios/RNFIRMesssaging.m | 10 +++++ 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/android/react-native-fcm.iml b/android/react-native-fcm.iml index 841500a..f1f4a60 100644 --- a/android/react-native-fcm.iml +++ b/android/react-native-fcm.iml @@ -65,14 +65,6 @@ - - - - - - - - @@ -81,12 +73,21 @@ + + + + + + + + + @@ -101,7 +102,6 @@ - @@ -122,10 +122,11 @@ + - + diff --git a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java index 219559e..96928ab 100644 --- a/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java +++ b/android/src/main/java/com/evollu/react/fcm/FIRMessagingModule.java @@ -13,6 +13,7 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; import com.google.firebase.iid.FirebaseInstanceId; +import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.RemoteMessage; import android.os.Bundle; @@ -71,6 +72,16 @@ public class FIRMessagingModule extends ReactContextBaseJavaModule implements Li promise.resolve(FirebaseInstanceId.getInstance().getToken()); } + @ReactMethod + public void subscribeToTopic(String topic){ + FirebaseMessaging.getInstance().subscribeToTopic(topic); + } + + @ReactMethod + public void unsubscribeFromTopic(String topic){ + FirebaseMessaging.getInstance().unsubscribeFromTopic(topic); + } + private void sendEvent(String eventName, Object params) { getReactApplicationContext() .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) diff --git a/index.js b/index.js index 208d253..3200504 100644 --- a/index.js +++ b/index.js @@ -1,37 +1,40 @@ -import { - NativeModules, - DeviceEventEmitter, -} from 'react-native'; +import {NativeModules, DeviceEventEmitter} from 'react-native'; const eventsMap = { - refersh: 'FCMTokenRefreshed', - notification: 'FCMNotificationReceived', + refreshToken: 'FCMTokenRefreshed', + notification: 'FCMNotificationReceived' }; const FIRMessaging = NativeModules.RNFIRMessaging; const FCM = {}; -FCM.getFCMToken = function getFCMToken() { +FCM.getFCMToken = () => { return FIRMessaging.getFCMToken(); }; -FCM.requestPermissions = function requestPermissions() { - return FIRMessaging.requestPermissions(); +FCM.requestPermissions = () => { + return FIRMessaging.requestPermissions(); }; -FCM.on = function on(event, callback) { - const nativeEvent = eventsMap[event]; +FCM.on = (event, callback) => { + const nativeEvent = eventsMap[event]; - const listener = DeviceEventEmitter.addListener(nativeEvent, (params) => { - callback(params); - }); + const listener = DeviceEventEmitter.addListener(nativeEvent, callback); - return function remove() { - listener.remove(); - }; + return function remove() { + listener.remove(); + }; }; +FCM.subscribeToTopic = (topic) => { + FIRMessaging.subscribeToTopic(topic); +} + +FCM.unsubscribeFromTopic = (topic) => { + FIRMessaging.unsubscribeFromTopic(topic); +} + FCM.initialData = FIRMessaging.initialData; FCM.initialAction = FIRMessaging.initialAction; diff --git a/ios/RNFIRMesssaging.m b/ios/RNFIRMesssaging.m index 7226796..98bf37e 100644 --- a/ios/RNFIRMesssaging.m +++ b/ios/RNFIRMesssaging.m @@ -109,6 +109,16 @@ RCT_EXPORT_METHOD(requestPermissions) } } +RCT_EXPORT_METHOD(subscribeToTopic: (NSString*) topic) +{ + [[FIRMessaging messaging] subscribeToTopic:topic]; +} + +RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic) +{ + [[FIRMessaging messaging] unsubscribeFromTopic:topic]; +} + - (void)handleRemoteNotificationReceived:(NSNotification *)notification { [_bridge.eventDispatcher sendDeviceEventWithName:FCMNotificationReceived -- 2.26.2