2017-05-29 74 views
0

一旦我再次打開應用程序,通知就會正常開始。以下是我的didFinish方法。應用程序24小時待機後未收到Firebase通知

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] 
} 
+0

閱讀我對這個問題的回答https://stackoverflow.com/questions/37899712/fcm-background-notifications-not-working-在-IOS/37899773#37899773 – Chris

回答

0

所以我終於找到了問題。

我重寫了AppDelegate的默認初始化程序來配置Firebase應用程序。之後,我沒有收到通知,

所以要解決這個問題,我以後該通知將啓動未來設定我的內didRegisterForRemoteNotification APNS的道理,但在24小時左右後只能暫時不會來

基本上,請不要覆蓋默認初始值設定項,除非你真的知道你在做什麼,

相關問題