应用程序待机24小时后未收到Firebase通知

时间:2017-05-29 08:45:50

标签: swift firebase time push-notification

一旦我再次打开应用程序,通知就会正常开始。以下是我的didFinish方法。

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

    // [CONFIGURE Firebase App]
    FirebaseApp.configure()

    // [ENABLE Firebase Persistance]
    Database.database().isPersistenceEnabled = true

    // Setting Api Key for Google maps
    GMSServices.provideAPIKey("AIzaSyDSBDWuW4U3655ifuS6H0LJ9conLrX9_5Q")

    let check =  UserDefaults.standard.value(forKey: "CHECK_LOGIN") as? String

    if check == "YES"
    {
        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "ROOTVIEW") as! DLDemoRootViewController
        self.window?.rootViewController = nextViewController

    }else
    {
        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "loginVIew") as! LoginVC
        self.window?.rootViewController = nextViewController
    }

    // Register for remote notifications. This shows a permission dialog on first run, to
    // show the dialog at a more appropriate time move this registration accordingly.
    // [START register_for_notifications]
    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 })
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    // Register for Remote Notification
    application.registerForRemoteNotifications()


    // [START set_messaging_delegate]
    Messaging.messaging().delegate = self
    // [END set_messaging_delegate]

    return true
}

// [START ios_10_data_message_handling]
extension AppDelegate : MessagingDelegate {

    // [START refresh_token]

    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {

        print("Firebase Refreshed registration token: \(fcmToken)")
        UserDefaults.standard.set(fcmToken, forKey: "token")

        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "tokenRefresh"), object: nil)
    }
     // [END ios_10_data_message]

    // [START ios_10_data_message]
    // Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground.
    // To enable direct data messages, you can set     Messaging.messaging().shouldEstablishDirectChannel to true.
    func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {

        print("Received data message: \(remoteMessage.appData)")
    }
    // [END ios_10_data_message]
}

1 个答案:

答案 0 :(得分:0)

所以我终于找到了问题。

我覆盖了AppDelegate的默认初始化程序以配置Firebase应用程序。之后我没有收到通知,

所以为了解决这个问题,我在ApRegisterForRemoteNotification中设置了我的Apns令牌,之后通知就会开始了,但是只有在24小时左右才会暂时出现

基本上,除非你真的知道自己在做什么,否则不要覆盖默认的初始值设定项,