0

我设置的代码在我的AppDelegate:为什么我没有从Swift的Firebase推送通知?

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate { 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    if #available(iOS 10.0, *) { 
     let authOptions: UNAuthorizationOptions = [.Alert, .Badge, .Sound] 
     UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions(authOptions, completionHandler: { (granted: Bool, error: NSError?) in 
      // For iOS 10 display notification (sent via APNS) 
      UNUserNotificationCenter.currentNotificationCenter().delegate = self 
      // For iOS 10 data message (sent via FCM) 
      FIRMessaging.messaging().remoteMessageDelegate = self 
     }) 
    } else { 
     let settings: UIUserNotificationSettings = 
      UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    } 

    application.registerForRemoteNotifications() 

    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil)) 
} 

而且这个部分:

internal func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
    print(userInfo) 
    // 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. 
    // TODO: Handle data of notification 

    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    // Print full message. 
    print(userInfo) 
} 

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], 
        fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
    // 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. 
    // TODO: Handle data of notification 

    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    // Print full message. 
    print(userInfo) 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 
    //Handle the notification 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 
    //Handle the notification 
} 

我还上传了发展的APN为苹果开发者。在Firebase控制台中,它写道通知已成功发送,但我没有收到任何推送通知。任何人都可以请帮我检测我的错误在哪里?

我已经为它工作了3天。搜索了很多,但没有。

回答

0
  1. 添加FIRApp.configure()didFinishLaunchingWithOptions:
  2. 添加FIRMessaging.messaging().connect { (error) in if error != nil { print("Unable to connect with FCM. \(error)") } else { print("Connected to FCM.") }didFinishLaunchingWithOptions:
  3. 贯彻到底的FIRMessageDelegate功能

    func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 
    
        print(remoteMessage.appData) 
    } 
    
  4. 添加FIRMessaging.messaging().appDidReceiveMessage(userInfo)func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

精加工代码应该是这样的:

import UIKit 
import Firebase 
import UserNotifications 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, FIRMessagingDelegate { 

var window: UIWindow? 


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    FIRApp.configure() 

    if #available(iOS 10.0, *) { 
     let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
     UNUserNotificationCenter.current().requestAuthorization(
      options: authOptions, 
      completionHandler: {_, _ in }) 

     // For iOS 10 display notification (sent via APNS) 
     UNUserNotificationCenter.current().delegate = self 
     // For iOS 10 data message (sent via FCM) 
     FIRMessaging.messaging().remoteMessageDelegate = self 
    } else { 
     let settings: UIUserNotificationSettings = 
      UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    } 


    FIRMessaging.messaging().connect { (error) in 
     if error != nil { 
      print("Unable to connect with FCM. \(error)") 
     } else { 
      print("Connected to FCM.") 
     } 
    } 
    application.registerForRemoteNotifications() 

    return true 
} 

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 

    print("applicationReceivedRemoteMessage ") 
    print(remoteMessage.appData) 
} 

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. 
    // TODO: Handle data of notification 

    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    // Print full message. 
    print(userInfo) 
} 

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
       fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    // 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. 
    // TODO: Handle data of notification 

    // Print message ID. 
    print("Message ID: \(userInfo["gcm.message_id"]!)") 

    // Print full message. 
    print(userInfo) 

    FIRMessaging.messaging().appDidReceiveMessage(userInfo) 
} 
+0

什么也没有发生。 Firebase写道发送成功,但我没有收到任何内容=/ –

+0

您是否可以从Firebase控制台屏蔽您的“撰写消息”屏幕?您是否正确设置了FCM注册令牌? –

+0

您是否从XCode控制台获得了“连接到FCM”? –