2017-05-29 85 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小时左右后只能暂时不会来

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

相关问题