diff --git a/docs/messaging/notifications.mdx b/docs/messaging/notifications.mdx index c42b4a1172..1d76291dbe 100644 --- a/docs/messaging/notifications.mdx +++ b/docs/messaging/notifications.mdx @@ -207,6 +207,12 @@ function App() { To send messages to a device, you would need the FCM token for it, which you can get using the `getToken(messaging)` method. An example is available on '[Notifee pages](https://notifee.app/react-native/docs/integrations/fcm)'. +On iOS, React Native Firebase Messaging automatically registers the device with APNs by default. You only need to +manually call `registerDeviceForRemoteMessages(messaging)` before `getToken(messaging)` if you disabled +auto-registration with `messaging_ios_auto_register_for_remote_messages` in `firebase.json`, or if you otherwise call +`getToken(messaging)` before the app has registered for remote messages. See +[Auto Registration (iOS)](/messaging/usage#auto-registration-ios) for the manual registration example. + ```jsx import { getMessaging, @@ -215,6 +221,7 @@ import { } from '@react-native-firebase/messaging'; const messaging = getMessaging(); +// Optional: not needed in most cases, and a no-op if already registered. await registerDeviceForRemoteMessages(messaging); const token = await getToken(messaging); // save the token to the db diff --git a/packages/app/firebase-schema.json b/packages/app/firebase-schema.json index 3733edd956..31896a46da 100644 --- a/packages/app/firebase-schema.json +++ b/packages/app/firebase-schema.json @@ -112,7 +112,7 @@ "type": "string" }, "messaging_ios_auto_register_for_remote_messages": { - "description": "Whether RNFirebase Messaging automatically calls `[[UIApplication sharedApplication] registerForRemoteNotifications];`\nautomatically on app launch (recommended) - defaults to true.\n If set to false; make sure to call `firebase.messaging().registerDeviceForRemoteMessages()`\nearly on in your app startup - otherwise you will NOT receive remote messages/notifications\nin your app.\n", + "description": "Whether RNFirebase Messaging automatically calls `[[UIApplication sharedApplication] registerForRemoteNotifications];`\non app launch (recommended) - defaults to true.\n If set to false, make sure to call `registerDeviceForRemoteMessages(getMessaging())`\nearly on in your app startup - otherwise you will NOT receive remote messages/notifications\nin your app.\n", "type": "boolean" }, "messaging_ios_foreground_presentation_options": { diff --git a/packages/messaging/ios/RNFBMessaging/RNFBMessagingModule.mm b/packages/messaging/ios/RNFBMessaging/RNFBMessagingModule.mm index 0cfe104c9c..228f2cdc77 100644 --- a/packages/messaging/ios/RNFBMessaging/RNFBMessagingModule.mm +++ b/packages/messaging/ios/RNFBMessaging/RNFBMessagingModule.mm @@ -125,15 +125,15 @@ - (void)getToken:(NSString *)appName resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { if ([UIApplication sharedApplication].isRegisteredForRemoteNotifications == NO) { - [RNFBSharedUtils rejectPromiseWithUserInfo:reject - userInfo:(NSMutableDictionary *)@{ - @"code" : @"unregistered", - @"message" : @"You must be registered for remote " - @"messages before calling " - @"getToken, see " - @"messaging()." - @"registerDeviceForRemoteMessages().", - }]; + [RNFBSharedUtils + rejectPromiseWithUserInfo:reject + userInfo:(NSMutableDictionary *)@{ + @"code" : @"unregistered", + @"message" : @"You must be registered for remote " + @"messages before calling " + @"getToken, see " + @"registerDeviceForRemoteMessages(getMessaging()).", + }]; return; } @@ -193,15 +193,15 @@ - (void)getAPNSToken:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlo @"Use setAPNSToken in testing if needed."); #endif if ([UIApplication sharedApplication].isRegisteredForRemoteNotifications == NO) { - [RNFBSharedUtils rejectPromiseWithUserInfo:reject - userInfo:(NSMutableDictionary *)@{ - @"code" : @"unregistered", - @"message" : @"You must be registered for remote " - @"messages before " - @"calling getAPNSToken, see " - @"messaging()." - @"registerDeviceForRemoteMessages().", - }]; + [RNFBSharedUtils + rejectPromiseWithUserInfo:reject + userInfo:(NSMutableDictionary *)@{ + @"code" : @"unregistered", + @"message" : @"You must be registered for remote " + @"messages before " + @"calling getAPNSToken, see " + @"registerDeviceForRemoteMessages(getMessaging()).", + }]; return; } resolve([NSNull null]);