firebase messaging - Register for remote notification

27 篇文章 0 订阅

转载需要翻墙看的文档,方便以后查阅。

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 ];
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 FIRMessagingdelegate 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.
}

Alternatively, you can listen for an NSNotification namedkFIRMessagingRegistrationTokenRefreshNotification 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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值