【问题标题】:Notification not coming via Firebase console通知未通过 Firebase 控制台发送
【发布时间】:2017-05-23 10:42:05
【问题描述】:

对于上述问题,我已经尝试了所有可能的解决方案,但仍未通过 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 警告:

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 推送通知已启用。
    • 上面的Log in 截图显示Failed to get APNS Token。请检查您是否收到了设备token并上传了正确的p12证书。
    【解决方案2】:

    我认为问题是因为 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()
        }
    }
    

    【讨论】:

    • 不确定您是否已完成此操作,但您需要进入 firebase 项目设置,选择云消息传递并为开发阶段上传 APNs 开发证书,以便接收 firebase 通知。您还需要一个真实的设备才能接收通知。无法在模拟器上进行通知测试。
    【解决方案3】:

    问题已为我解决。 我跳过了上传 APNs 开发证书。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2017-08-24
      • 2017-07-01
      • 1970-01-01
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多