Skip to content
Merged
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
7 changes: 7 additions & 0 deletions docs/messaging/notifications.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Comment thread
wanxiankai marked this conversation as resolved.
```jsx
import {
getMessaging,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/app/firebase-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
36 changes: 18 additions & 18 deletions packages/messaging/ios/RNFBMessaging/RNFBMessagingModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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]);
Expand Down
Loading