0

我已經在我的項目中成功添加了Firebase和通知。 Using this通知即將到來,我可以通過點擊該通知來訪問它們。但問題是,如果我的應用程序關閉了,我收到了一個通知,現在不用點擊就清除了。現在沒有數據插入我的Db中。如何保持隊列中的所有通知直接在下次打開應用程序時訪問。提前致謝。FCM通知問題 - swift3

class AppDelegate: UIResponder, UIApplicationDelegate , CLLocationManagerDelegate , FIRMessagingDelegate , UNUserNotificationCenterDelegate { 

var window: UIWindow? 
var locationManager = CLLocationManager() 
var utill = FencingUtil() 
var wrap = WrapperApi() 
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 
    if launchOptions != nil{ 
     let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] 
     if userInfo != nil { 
      self.handleUserInfo(userinfo: userInfo as! [AnyHashable : Any]) 
      // Perform action here 
     } 
    } 
    self.locationManager.delegate = self 
    self.locationManager.requestWhenInUseAuthorization() 
    self.locationManager.distanceFilter = 50.0; // Will notify the LocationManager every 100 meters 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    GMSServices.provideAPIKey("AIzaSyDNoFY_sbP9j-ObZx06B_rmjcCVrCM_ZP0") 
    GMSPlacesClient.provideAPIKey("AIzaSyDNoFY_sbP9j-ObZx06B_rmjcCVrCM_ZP0") 

    FIRApp.configure() 
    // var db: SQLiteDatabase 
    let dataStore = SQLiteDataStore.sharedInstance 
      do { 
       try dataStore.createTables() 
      } 
      catch _{} 
    if #available(iOS 10.0, *) { 
     let center = UNUserNotificationCenter.current() 
     center.delegate = self 
     center.requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(grant, error) in 
      if error == nil { 
       if grant { 
        application.registerForRemoteNotifications() 
       } else { 
        //User didn't grant permission 
       } 
      } else { 
       print("error: ",error) 
      } 
     }) 
     // For iOS 10 display notification (sent via APNS) 

    } else { 
     let settings: UIUserNotificationSettings = 
     UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
     application.registerUserNotificationSettings(settings) 
    } 

    application.registerForRemoteNotifications() 

    return true 
} 
@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    print("Handle push from foreground") 
    // custom code to handle push while app is in the foreground 
    //print("\(notification.request.content.userInfo)") 
    self.handleUserInfo(userinfo: notification.request.content.userInfo) 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    print("Handle push from background or closed") 
    // if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background 

    self.handleUserInfo(userinfo: response.notification.request.content.userInfo) 
} 

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) 
{ 
    print("%@", remoteMessage.appData) 
} 
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) 
{ self.application(application, didReceiveRemoteNotification: userInfo) { (UIBackgroundFetchResult) in } 
    print ("dasdasd") 

} 

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
       fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
self.handleUserInfo(userinfo: userInfo) 

} 
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox) 
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.prod) 



} 
func tokenRefreshNotificaiton(_ notification: Foundation.Notification) { 
    guard let refreshedToken = FIRInstanceID.instanceID().token() 
     else { 
      return 
    } 
    //refreshedToken = FIRInstanceID.instanceID().token()! 

    print("InstanceID token: \(refreshedToken)") 

    utill.tokenDefault.setValue(refreshedToken, forKey: "tokenId") 
    // Connect to FCM since connection may have failed when attempted before having a token. 
    connectToFcm() 
} 
// [START connect_to_fcm] 
func connectToFcm() { 
    FIRMessaging.messaging().connect { (error) in 
     if (error != nil) { 
      print("Unable to connect with FCM. \(String(describing: error))") 
     } else { 
      //print("Connected to FCM.") 
     } 
    } 
}} 
+0

你是說你已經清除了通知中心的通知,並且在刪除之後你想把它保存在數據庫中? – Pushpendra

+0

是的,當應用程序處於後臺模式時是否可以直接訪問通知? –

+0

如果您要從通知中心移除通知,則無法獲取notification.check對象的答案,以便在didfinish啓動時獲取對象。 – Pushpendra

回答

1

,如果你正在啓動應用程序,然後你可以在didFinishLaunchingWithOptions方法,你通知對象試試這個。如果您有 未清除通知中心的通知。

 if (launchOptions != nil) { 
     if let remoteNotification = launchOptions? 
     [UIApplicationLaunchOptionsKey.remoteNotification] { 
      let objNotification = remoteNotification as! [AnyHashable : 
     Any] 
      } 
    } 
+0

Launghing應用程序不會調用此函數。 –

+0

請分享您的代碼。 – Pushpendra