diff --git a/index.d.ts b/index.d.ts index 06bdd536..9e5ea555 100644 --- a/index.d.ts +++ b/index.d.ts @@ -532,6 +532,23 @@ export interface PushNotificationIOSStatic { * Used to set specific actions for notifications that contains specified category */ setNotificationCategories(categories: NotificationCategory[]): void; + + /** + * Subscribes to the given topic (works only with Firebase). + * @param topic the topic + */ + subscribeToTopic(topic: string): void; + + /** + * Unsubscribes from the given topic (works only with Firebase). + * @param topic the topic + */ + unsubscribeFromTopic(topic: string): void; + + /** + * Gets the FCM token for the device (works only with Firebase). + */ + getFCMToken(): Promise; } declare const PushNotificationIOS: PushNotificationIOSStatic; diff --git a/ios/RNCPushNotificationIOS.m b/ios/RNCPushNotificationIOS.m index bc6bb3d3..7ba2e43a 100644 --- a/ios/RNCPushNotificationIOS.m +++ b/ios/RNCPushNotificationIOS.m @@ -10,6 +10,7 @@ #import #import #import +#import NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived"; @@ -510,6 +511,39 @@ - (void)loadAttachmentForUrl:(NSURL *)attachmentURL } } +RCT_EXPORT_METHOD(subscribeToTopic: (NSString *)topic + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) { + + [[FIRMessaging messaging] subscribeToTopic:topic]; +} + +RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString *)topic + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) { + + [[FIRMessaging messaging] unsubscribeFromTopic:topic]; +} + +RCT_EXPORT_METHOD(getFCMToken:(RCTPromiseResolveBlock)resolve + reject:(RCTPromiseRejectBlock)reject) { + [[FIRMessaging messaging] tokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) { + if (error) { + // Handle the error, if any + NSLog(@"Error retrieving FCM token: %@", error.localizedDescription); + + reject(@"-1", error.localizedDescription, error); + + } else if (token) { + // FCM token successfully retrieved + resolve(token); + } else { + // Unable to retrieve FCM token + reject(@"-1", @"Error - Get FCM token failed.", error); + } + }]; +} + #else //TARGET_OS_TV RCT_EXPORT_METHOD(onFinishRemoteNotification:(NSString *)notificationId fetchResult:(NSString *)fetchResult) diff --git a/js/index.js b/js/index.js index b2cd9502..20f01ec7 100644 --- a/js/index.js +++ b/js/index.js @@ -456,6 +456,28 @@ class PushNotificationIOS { ); } + /** + * Subscribes to the given topic (works only with Firebase). + */ + static subscribeToTopic(topic: string) { + RNCPushNotificationIOS.subscribeToTopic(topic); + } + + /** + * Unsubscribes from the given topic (works only with Firebase). + * @param topic the topic + */ + static unsubscribeFromTopic(topic: string) { + RNCPushNotificationIOS.unsubscribeFromTopic(topic); + } + + /** + * Gets the FCM token for the device (works only with Firebase). + */ + static getFCMToken(): Promise { + return RNCPushNotificationIOS.getFCMToken(); + } + /** * You will never need to instantiate `PushNotificationIOS` yourself. * Listening to the `notification` event and invoking