1

私はSwiftプロジェクトに取り組み、FCMをFCMに統合しています。私は、アプリが実行されているときだけでなく、アプリがバックグラウンドの状態にあるときにプッシュ通知を受け取ることができます。しかし、アプリケーションを終了(強制終了)するとコンソールから通知を送信するときに通知が表示されないことがあります。iOS FCMプッシュ通知が受信されませんでした。アプリが終了した場合

私は、iOS 10に取り組んでdidFinishLaunchingWithOptions内のコードの下に実装されています:

UNUserNotificationCenter.current().delegate = self 

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in 
    if error == nil { 
      UIApplication.shared.registerForRemoteNotifications() 

      if granted { 
       print("Notification access true!") 
      } else { 
       print("Notification access false") 
      } 
     } 
} 

私もUNUserNotificationCenterDelegateを実装して、それは方法です。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    completionHandler([.alert,.badge, .sound]) 
} 

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     let userInfo = response.notification.request.content.userInfo 

     print(userInfo) 

     self.handleOnNotifClick() 

     completionHandler() 
} 

アプリが最近使用されたアプリの引き出しから強制終了された場合にのみ発生します。それを調べてください。どんな助けもありがとう。

+0

アプリが終了すると、アプリデリゲートに通知が届きません。通知に警告ペイロードが含まれている場合は、これが表示されます。 – Paulw11

+0

ありがとう@ Paulw11.But私は既にアラートでデータを送信しています。通知は表示されません。 –

+0

はフォアグラウンドモードで受信していますか? –

答えて

1

解決済み!

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    let state: UIApplicationState = UIApplication.shared.applicationState 

    if state == .active{ 
     if let notification = userInfo["aps"] as? [AnyHashable: Any], 
      let alert = notification["alert"] as? String { 
      print(alert) 
      let localNotification = UILocalNotification() 
      localNotification.alertBody = alert 
      localNotification.soundName = UILocalNotificationDefaultSoundName 
      UIApplication.shared.scheduleLocalNotification(localNotification) 
     } 
    }else if state == .inactive{ 

     if let notification = userInfo["aps"] as? [AnyHashable: Any],let alert = notification["alert"] as? String { 
      print(alert) 
      let localNotification = UILocalNotification() 
      localNotification.alertBody = alert 
      localNotification.soundName = UILocalNotificationDefaultSoundName 
      UIApplication.shared.scheduleLocalNotification(localNotification) 

     } 
    }else if state == .background{ 
     UIApplication.shared.applicationIconBadgeNumber = 0 

     if let notification = userInfo["aps"] as? [AnyHashable: Any],let alert = notification["alert"] as? String,let sound = notification["sound"] as? String{ 
      print(alert) 
      var localNotification = UILocalNotification() 
      localNotification.alertBody = alert 
      localNotification.soundName = sound 
      UIApplication.shared.scheduleLocalNotification(localNotification) 
     } 
    } 
} 
関連する問題