Firebase不会显示通知

时间:2016-07-18 20:05:32

标签: ios swift firebase firebase-cloud-messaging firebase-notifications

所以我刚刚为我的应用实现了(很好地尝试)推送通知。我已经将所有证书分类并在Xcode中运行所有内容。我在开发部分将我的.p12文件上传到Firebase,甚至下载了配置文件并将其重新安装到我的项目中。

这是我的AppDelegate.swift文件

中的代码

更新的代码:

import UIKit
import Firebase
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var storyboard: UIStoryboard?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        FIRApp.configure()

        // Override point for customization after application launch.

        self.storyboard =  UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let currentUser = FIRAuth.auth()?.currentUser
        if currentUser != nil
        {
            self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("tBVC")
        }
        else
        {
            self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("loginScreen")
        }

        return true
    }

    func registerForPushNotifications(application: UIApplication) {
        let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)
    }

    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        if notificationSettings.types != .None {
            application.registerForRemoteNotifications()
        }
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

        print(userInfo)
        print("MessageID:  \(userInfo["gcm_message_id"]!)")
        //
    }


}

当应用程序在后台运行时,即使我的设备仍未锁定,也不会显示任何通知。警报弹出,询问应用程序是否有权发送通知,我说是。

我遵循了tutorial

知道为什么我的通知没有显示? 在Firebase控制台中,它表示通知的状态为“已完成”

编辑 - 在Xcode中添加了我的功能图像 enter image description here

2 个答案:

答案 0 :(得分:1)

我找出了为什么会这样做的原因!正如@Collinizer所述,Apple和APNS存在问题,但它现在都在运行!我在使用OneSignal时添加了推送通知,他们正在做梦!

感谢所有帮助过的人:)

答案 1 :(得分:0)

您需要配置APNS设备令牌的设置,这对于使用Firebase云消息传递推送通知至关重要( FCM )。

首先让我们稍微回顾一下,看看我们是否至少能获得令牌成功。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

FIRApp.configure()
return true
}

func registerForPushNotifications(application: UIApplication) {
    let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}


func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""

for i in 0..<deviceToken.length {
    tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}


FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
print("Device Token:", tokenString)
}