iOS – ObjectiveC
📲 Handling FCM Token Registration in iOS
🔧 Objective-C Implementation in AppDelegate.m
AppDelegate.m
The following code snippet captures the FCM registration token from Firebase and makes it available for use throughout the application or for SDK integrations like REL-ID:
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end
@implementation AppDelegate
@property (nonatomic, strong) NSString *deviceToken;
- (void)messaging:(FIRMessaging *)messaging
didReceiveRegistrationToken:(NSString *)fcmToken {
NSLog(@"FCM registration token: %@", fcmToken);
// Notify other parts of the app with the new token
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"FCMToken" object:nil userInfo:dataDict];
// TODO: If necessary, send the token to the application backend server
// Save the token locally to provide it when requested (e.g., by the REL-ID SDK)
self.deviceToken = fcmToken;
}
📌 Key Functionalities
Feature | Description |
---|---|
didReceiveRegistrationToken: | Called when Firebase generates or refreshes the FCM token. |
NSLog | Logs the received token for debugging. |
NSNotificationCenter | Broadcasts the new token across the app for any listening components. |
self.deviceToken = fcmToken | Caches the token for later use by SDKs like REL-ID. |
🧠 Notes
- This method is automatically invoked:
- On first app launch after install.
- Whenever the FCM token is refreshed.
- To expose this token to the REL-ID SDK, implement the
getDeviceToken
method as shown below:
- (NSString*)getDeviceToken {
AppDelegate *delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
return delegate.deviceToken;
}
🔁 Next Steps
- Ensure FCM is properly configured with Firebase in your app.
- Register for push notifications in
didFinishLaunchingWithOptions
. - Implement secure backend transmission of the token if required.
Updated 2 months ago