Skip to content

Commit

Permalink
fix(messaging, getToken): add options for messaging instance
Browse files Browse the repository at this point in the history
Adds `projectId` and `senderId` token options to set messaging instance to use with FCM
  • Loading branch information
ecexplorer authored and mikehardy committed May 26, 2022
1 parent dd2d1ee commit 88e218e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
import io.invertase.firebase.common.ReactNativeFirebaseEventEmitter;
Expand Down Expand Up @@ -120,8 +121,9 @@ public void setAutoInitEnabled(Boolean enabled, Promise promise) {
}

@ReactMethod
public void getToken(Promise promise) {
Tasks.call(getExecutor(), () -> Tasks.await(FirebaseMessaging.getInstance().getToken()))
public void getToken(String appName, String senderId, Promise promise) {
FirebaseMessaging messagingInstance = FirebaseApp.getInstance(appName).get(FirebaseMessaging.class);
Tasks.call(getExecutor(), () -> Tasks.await(messagingInstance.getToken()))
.addOnCompleteListener(
task -> {
if (task.isSuccessful()) {
Expand All @@ -133,11 +135,12 @@ public void getToken(Promise promise) {
}

@ReactMethod
public void deleteToken(Promise promise) {
public void deleteToken(String appName, String senderId, Promise promise) {
FirebaseMessaging messagingInstance = FirebaseApp.getInstance(appName).get(FirebaseMessaging.class);
Tasks.call(
getExecutor(),
() -> {
Tasks.await(FirebaseMessaging.getInstance().deleteToken());
Tasks.await(messagingInstance.deleteToken());
return null;
})
.addOnCompleteListener(
Expand Down
43 changes: 27 additions & 16 deletions packages/messaging/ios/RNFBMessaging/RNFBMessagingModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ - (NSDictionary *)constantsToExport {
}
}

RCT_EXPORT_METHOD(getToken : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) {
RCT_EXPORT_METHOD(getToken
: (NSString *)appName
: (NSString *)senderId
: (RCTPromiseResolveBlock)resolve
: (RCTPromiseRejectBlock)reject) {
#if !(TARGET_IPHONE_SIMULATOR)
if ([UIApplication sharedApplication].isRegisteredForRemoteNotifications == NO) {
[RNFBSharedUtils
Expand All @@ -123,23 +127,30 @@ - (NSDictionary *)constantsToExport {
#endif

[[FIRMessaging messaging]
tokenWithCompletion:^(NSString *_Nullable token, NSError *_Nullable error) {
if (error) {
[RNFBSharedUtils rejectPromiseWithNSError:reject error:error];
} else {
resolve(token);
}
}];
retrieveFCMTokenForSenderID:senderId
completion:^(NSString *_Nullable token, NSError *_Nullable error) {
if (error) {
[RNFBSharedUtils rejectPromiseWithNSError:reject error:error];
} else {
resolve(token);
}
}];
}

RCT_EXPORT_METHOD(deleteToken : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) {
[[FIRMessaging messaging] deleteTokenWithCompletion:^(NSError *_Nullable error) {
if (error) {
[RNFBSharedUtils rejectPromiseWithNSError:reject error:error];
} else {
resolve([NSNull null]);
}
}];
RCT_EXPORT_METHOD(deleteToken
: (NSString *)appName
: (NSString *)senderId
: (RCTPromiseResolveBlock)resolve
: (RCTPromiseRejectBlock)reject) {
[[FIRMessaging messaging] deleteFCMTokenForSenderID:senderId
completion:^(NSError *_Nullable error) {
if (error) {
[RNFBSharedUtils rejectPromiseWithNSError:reject
error:error];
} else {
resolve([NSNull null]);
}
}];
}

RCT_EXPORT_METHOD(getAPNSToken : (RCTPromiseResolveBlock)resolve : (RCTPromiseRejectBlock)reject) {
Expand Down
30 changes: 26 additions & 4 deletions packages/messaging/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,25 @@ export namespace FirebaseMessagingTypes {
threadId?: string;
}

/**
* Options for `getToken()`, `deleteToken()`
*/
export interface TokenOptions {
/**
* The app name of the FirebaseApp instance.
*
* @platform android Android
*/
appName?: string;

/**
* The senderID for a particular Firebase project.
*
* @platform ios iOS
*/
senderId?: string;
}

export interface Notification {
/**
* The notification title.
Expand Down Expand Up @@ -579,8 +598,7 @@ export namespace FirebaseMessagingTypes {
getDidOpenSettingsForNotification(): Promise<boolean>;

/**
* Returns an FCM token for this device. Optionally you can specify a custom authorized entity
* or scope to tailor tokens to your own use-case.
* Returns an FCM token for this device. Optionally you can specify a custom options to your own use-case.
*
* It is recommended you call this method on app start and update your backend with the new token.
*
Expand All @@ -602,8 +620,10 @@ export namespace FirebaseMessagingTypes {
* fcmTokens: firebase.firestore.FieldValues.arrayUnion(fcmToken),
* });
* ```
*
* @param options Options to override senderId (iOS) and projectId (Android).
*/
getToken(): Promise<string>;
getToken(options?: TokenOptions): Promise<string>;

/**
* Returns wether the root view is headless or not
Expand All @@ -623,8 +643,10 @@ export namespace FirebaseMessagingTypes {
* ```js
* await firebase.messaging().deleteToken();
* ```
*
* @param options Options to override senderId (iOS) and projectId (Android).
*/
deleteToken(): Promise<void>;
deleteToken(options?: TokenOptions): Promise<void>;

/**
* When any FCM payload is received, the listener callback is called with a `RemoteMessage`.
Expand Down
31 changes: 27 additions & 4 deletions packages/messaging/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
isIOS,
isObject,
isString,
isUndefined,
} from '@react-native-firebase/app/lib/common';
import {
createModuleNamespace,
Expand Down Expand Up @@ -154,12 +155,34 @@ class FirebaseMessagingModule extends FirebaseModule {
return this.native.getIsHeadless();
}

getToken() {
return this.native.getToken();
getToken({ appName, senderId } = {}) {
if (!isUndefined(appName) && !isString(appName)) {
throw new Error("firebase.messaging().getToken(*) 'projectId' expected a string.");
}

if (!isUndefined(senderId) && !isString(senderId)) {
throw new Error("firebase.messaging().getToken(*) 'senderId' expected a string.");
}

return this.native.getToken(
appName || this.app.name,
senderId || this.app.options.messagingSenderId,
);
}

deleteToken() {
return this.native.deleteToken();
deleteToken({ appName, senderId } = {}) {
if (!isUndefined(appName) && !isString(appName)) {
throw new Error("firebase.messaging().deleteToken(*) 'projectId' expected a string.");
}

if (!isUndefined(senderId) && !isString(senderId)) {
throw new Error("firebase.messaging().deleteToken(*) 'senderId' expected a string.");
}

return this.native.deleteToken(
appName || this.app.name,
senderId || this.app.options.messagingSenderId,
);
}

onMessage(listener) {
Expand Down

1 comment on commit 88e218e

@vercel
Copy link

@vercel vercel bot commented on 88e218e May 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.