0

我已按照其網站上的說明實施Firebase雲消息傳遞。所以,當我從這裏生成通知: enter image description heredidReceiveRemoteNotification未通過FCM接收通知

而在AppDelegate.swift我已經添加didReceiveRemoteNotification功能:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 

    fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 

     Messaging.messaging().appDidReceiveMessage(userInfo) 
     print(userInfo) 

     completionHandler(UIBackgroundFetchResult.newData) 
    } 

其他方法,我實現是
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String)
func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

鑑於我的didFinishLaunchingWithOptions有:

if #available(iOS 10.0, *) { 
      // For iOS 10 display notification (sent via APNS) 
      UNUserNotificationCenter.current().delegate = self 
      let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
      UNUserNotificationCenter.current().requestAuthorization(
       options: authOptions, 
       completionHandler: {_, _ in }) 
      // For iOS 10 data message (sent via FCM 
      Messaging.messaging().delegate = self 
     } else { 
      let settings: UIUserNotificationSettings = 
       UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
      application.registerUserNotificationSettings(settings) 
     } 

     application.registerForRemoteNotifications() 

     FirebaseApp.configure() 

     NotificationCenter.default.addObserver(self, selector: #selector(self.tokenRefreshNotification), name: NSNotification.Name.InstanceIDTokenRefresh, object: nil) 

     return true 

回答

1

請看看這個要點文件 AppDelegate file for FCM

在Xcode轉到目標>功能&確保您的推送通知是。 enter image description here 希望它有幫助。