Skip to content

feat: add support to get fcm token #410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
}

declare const PushNotificationIOS: PushNotificationIOSStatic;
Expand Down
34 changes: 34 additions & 0 deletions ios/RNCPushNotificationIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import <React/RCTBridge.h>
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <Firebase/Firebase.h>

NSString *const RCTRemoteNotificationReceived = @"RemoteNotificationReceived";

Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
return RNCPushNotificationIOS.getFCMToken();
}

/**
* You will never need to instantiate `PushNotificationIOS` yourself.
* Listening to the `notification` event and invoking
Expand Down