简体   繁体   中英

Firebase FCM on IOS 10 clarification

Is there anyone who can clarify the required methods for FCM using Swift 4.2 with an IOS Target of >= 10 to simplify: 1) no direct “data” msgs …. only notify/alert msgs thru APNS 2) I'm leaving Swizzling enabled even though some tutorials (without explaining why) tell me to turn it off

I'm finding that the IOS deprecation issues and some documentation omissions on the FCM side make this way more confusing than it needs to be.

can we get a simple matrix like: AppState | closed | background | foreground |

userAction: none (simple message arrival at IOS) dismiss notification tap notification select notification action open app directly -- no engagement with the notification

method being called in the above scenario is: blah …

also, it seems FCM creates it's own token regardless of whether APNS has sent one yet; so I need different logic to detect if user has granted permissions before my server can assume that the FCM token will work at all … I guess I just won't send FCM token to the server until I'm also sure I have an APNS token …. ???

Also, FCM docs make it clear that user-visible (ie Alert/Notification style) payloads are delivered THRU APNS/Apple.

Therefore, I think I can assume that FCM messages will not arrive until AFTER Apple has send me an APNS token.

But I seem unable to read or retrieve the Apple token from the FCM delegate method.

This code always returns an empty string for apns so I can't tell if my app is in a reliable state or not:

@objc func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print("Firebase registration token: (fcmToken)") let apnsTokenData = messaging.apnsToken let apnsToken = apnsTokenData?.reduce("", {$0 + String(format: "%02X", $1)}) ?? "" print("apnsToken:(apnsToken)")

Thanks for any clarifications!

Following are the functions which are mendatory to implement for APNS with firebase

  • To Register For Push Notifications
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
 UNUserNotificationCenter.current().requestAuthorization(
   options: authOptions,
   completionHandler: {_, _ in })
application.registerForRemoteNotifications()
  • To Receive FCM Token
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
}
  • While Receiving notification in Background State: If your application has received notification while in Background state , on clicking on that notification will pass the content of that notification to the underlying function.
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
  // If you are receiving a notification message while your app is in the background,
  // this callback will not be fired till the user taps on the notification launching the application.

}

  • While receiving application is in Foreground state: If your application has received notification while in Foreground state , on clicking on that notification will pass the content of that notification to the underlying function.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {

}

Q. I'm leaving Swizzling enabled even though some tutorials (without explaining why) tell me to turn it off: A. Method Swizzling is necassary to map the fcm token to apns token. If you have turned off method swizzling, you need to explicitly map fcm token to apns token like the following code

  Messaging.messaging().apnsToken = deviceToken

Q. can we get a simple matrix like: AppState | closed | background | foreground |: For any application state, the callback functions are already present in Appdelegate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM