轉載需要翻牆看的文檔,友善以後查閱。
Register for remote notifications
Either at startup, or at the desired point in your application flow, register your app for remote notifications. Call
registerForRemoteNotifications
as shown:
SWIFT
OBJECTIVE-C
if ( floor ( NSFoundationVersionNumber ) <= NSFoundationVersionNumber_iOS_9_x_Max ) {
UIUserNotificationType allNotificationTypes =
( UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge );
UIUserNotificationSettings * settings =
[ UIUserNotificationSettings settingsForTypes : allNotificationTypes categories : nil ];
[ application registerUserNotificationSettings : settings ];
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// For iOS 10 display notification (sent via APNS)
[ UNUserNotificationCenter currentNotificationCenter ]. delegate = self ;
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge ;
[[ UNUserNotificationCenter currentNotificationCenter ] requestAuthorizationWithOptions : authOptions completionHandler :^( BOOL granted , NSError * _Nullable error ) {
}];
#endif
}
[ application registerForRemoteNotifications ]; AppDelegate.m for devices running iOS 10 and above, you must assign your delegate object to the
UNUserNotificationCenter
object to receive display notifications, and the
FIRMessaging
object to receive data messages, before your app finishes launching. For example, in an iOS app, you must assign it in the
applicationWillFinishLaunching:
or
applicationDidFinishLaunching:
method.
Access the registration token
By default, the FCM SDK generates a registration token for the client app instance on initial startup of your app. Similar to the APNs device token, this token allows you to target notification messages to this particular instance of the app.
In the same way that iOS typically delivers an APNs device token on app start, FCM provides a registration token via the
messaging:didReceiveRegistrationToken
callback of the
FIRMessaging
delegate on each app startup. During the first app start, and in all situations where the registration token is changed, the FCM SDK retrieves the token. In both cases, the FCM SDK calls
messaging:didReceiveRegistrationToken:
found in the
FIRMessageDelegate
protocol.
The registration token may change when:
- The app is restored on a new device
- The user uninstalls/reinstall the app
- The user clears app data.
Set the messaging delegate
To receive registration tokens on app start, implement the messaging delegate protocol in a class and provide it to the delegate property after calling
[FIRApp configure]
. For example, if your application delegate conforms to the messaging delegate protocol, you can set the delegate on
application:didFinishLaunchingWithOptions:
to itself.
SWIFT
OBJECTIVE-C
[ FIRMessaging messaging ]. delegate = self ;
Receive the current registration token
Registration tokens are delivered via the method
messaging:didReceiveRegistrationToken:
. This method is called generally once per app start with an FCM token. When this method is called, it is the ideal time to:
- If the registration token is new, send it to your application server (it's recommended to implement server logic to determine whether the token is new).
- Subscribe the registration token to topics. This is required only for new subscriptions or for situations where the user has re-installed the app.
SWIFT
OBJECTIVE-C
NSString * fcmToken = [ FIRMessaging messaging ]. FCMToken ;
NSLog (@ "FCM registration token: %@" , fcmToken ); After this delegate method is called, the registration token is available via the token property (
FCMToken
for Objective-C,
fcmToken
for Swift). Prior to this delegate method call, the property may be nil.
Monitor token generation
To be notified whenever the token is updated, supply a delegate conforming to the messaging delegate protocol. The following example registers the delegate and adds the proper delegate method:
SWIFT
OBJECTIVE-C
- ( void ) messaging :( FIRMessaging *) messaging didReceiveRegistrationToken :( NSString *) fcmToken {
NSLog (@ "FCM registration token: %@" , fcmToken );
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
} AppDelegate.m
Alternatively, you can listen for an
NSNotification
named
kFIRMessagingRegistrationTokenRefreshNotification
rather than supplying a delegate method. The token property always has the current token value.
Swizzling disabled: mapping your APNs token and registration token
If you have disabled method swizzling, you'll need to explicitly map your APNs token to the FCM registration token. Override the methods
didRegisterForRemoteNotificationsWithDeviceToken
to retrieve the APNs token, and then use the
APNSToken
property.
Provide your APNs token using the
APNSToken
property:
SWIFT
OBJECTIVE-C
// With "FirebaseAppDelegateProxyEnabled": NO
- ( void ) application :( UIApplication *) application
didRegisterForRemoteNotificationsWithDeviceToken :( NSData *) deviceToken {
[ FIRMessaging messaging ]. APNSToken = deviceToken ;
}
After the FCM registration token is generated, you can access it and listen for refresh events using the same methods as with swizzling enabled.
Import existing user APNs tokens
If you have an existing user base that you want to onboard to an FCM client app, use the batchImportAPI provided by Instance ID. With this API, you can bulk import existing iOS APNs tokens into FCM, mapping them to new, valid registration tokens.
Next steps
After you have set up your iOS client, you're ready to add message handling and other, more advanced behavior to your app. See these guides for more information:
- Receive messages in an iOS app
- Send topic messages
- Send to device groups
- Send upstream messages