通知不会通过Firebase控制台发送

时间:2017-05-23 10:42:06

标签: ios swift firebase push-notification firebase-cloud-messaging

enter image description here enter image description here

我已针对上述问题尝试了所有可能的解决方案,但仍未通过Firebase控制台通知我的设备。请建议。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {

        self.handleNotification(remoteNotification as! [NSObject : AnyObject])
    }
    let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    // Add observer for InstanceID token refresh callback.
    NSNotificationCenter
        .defaultCenter()
        .addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotificaiton),
                     name: kFIRInstanceIDTokenRefreshNotification, object: nil)
return true }
 func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                 fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {


    FIRMessaging.messaging().appDidReceiveMessage(userInfo)



    NSNotificationCenter.defaultCenter().postNotificationName("reloadTheTable", object: nil)


}

 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}
func tokenRefreshNotificaiton(notification: NSNotification) {
    guard let refreshedToken = FIRInstanceID.instanceID().token()
        else {
            return
    }


    print("InstanceID token: \(refreshedToken)")

    utill.tokenDefault.setValue(refreshedToken, forKey: "tokenId")

    connectToFcm()
}

调试器中也显示很少的firebase警告:

3 个答案:

答案 0 :(得分:0)

如果您要从Xcode 8或更高版本部署应用程序,请确保已从图片中显示的项目目标功能部分启用了推送通知功能。 enter image description here

答案 1 :(得分:0)

我认为问题是因为ios版本。 ios 10需要UNUserNotificationCenter。尝试下面的代码,并将该函数添加到您的应用程序didFinishLaunching。

    func registerForRemoteNotification() {
    if #available(iOS 10.0, *) {
        let center  = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
            if error == nil{
                UIApplication.shared.registerForRemoteNotifications()
            }
        }
    }
    else {
        UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
        UIApplication.shared.registerForRemoteNotifications()
    }
}

答案 2 :(得分:0)

enter image description here问题已解决。 我跳过上传APNs开发证书。 enter image description here