0

FCMを使用してプッシュ通知を実装しました。 私のアプリがフォアグラウンドにあり、通知が届いたらdidReceiveRemoteNotificationメソッドが呼び出されますが、アプリケーションがアクティブでないときはこのメソッドは呼び出されませんプッシュ通知didReceiveRemoteNotification swift

私は、アプリが入っているときにbool値をtrueに設定してチェックしていますバックグラウンドがboolを変更しないことは、この行を実行しないことを意味します。 ここに私の方法です。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) { 
     self.application(application, didReceiveRemoteNotification: userInfo) { (UIBackgroundFetchResult) in 
      if UIApplication.sharedApplication().applicationState != .Active{ 


      NSUserDefaults.standardUserDefaults().setBool(true, forKey: "AriveNotification") 
      NSUserDefaults.standardUserDefaults().synchronize() 
      }else{ 

       NSUserDefaults.standardUserDefaults().setBool(false, forKey: "AriveNotification") 
       NSUserDefaults.standardUserDefaults().synchronize() 
      } 
     } 
    } 


    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 
     if UIApplication.sharedApplication().applicationState != .Active{ 


      NSUserDefaults.standardUserDefaults().setBool(true, forKey: "AriveNotification") 
      NSUserDefaults.standardUserDefaults().synchronize() 
      }else{ 

       NSUserDefaults.standardUserDefaults().setBool(false, forKey: "AriveNotification") 
       NSUserDefaults.standardUserDefaults().synchronize() 
      } 
     } 
     completionHandler(.NewData) 
    } 

GCMプッシュ通知を登録するにはユーザーが正常にサインインするときにこれらの方法を使用します。

func userDidLoggedIn() -> Void { 
    tabBarController = storyboard.instantiateViewControllerWithIdentifier("TabBarController") as! VaboTabBarController 


    self.window?.rootViewController = self.tabBarController 
    self.window?.makeKeyAndVisible() 

    self.registerDeviceForPushNotification(UIApplication.sharedApplication()) 

} 
func connectToFcm() { 

    FIRMessaging.messaging().connectWithCompletion({error in 


     if (error != nil) { 
      print("Unable to connect with FCM. \(error)") 
     } else { 
      print("Connected to FCM.") 
     } 


    }) 
} 
func application(application: UIApplication, 
       didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { 
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox) 
} 


func registerDeviceForPushNotification(application:UIApplication) -> Void { 

    let settings: UIUserNotificationSettings = UIUserNotificationSettings.init(forTypes: [.Alert,.Badge,.Sound], categories: nil) 
    self.pushNotificationToken = FIRInstanceID.instanceID().token()! 
    let userID = self.userData["id"] as! NSNumber 

    print("InstanceID token: \(self.pushNotificationToken)") 
    self.registerDeviceOnServerWith(self.pushNotificationToken, userID: userID) 
    application.registerUserNotificationSettings(settings) 
    application.registerForRemoteNotifications() 
} 


func tokenRefreshNotification(notification: NSNotification) { 
    if let refreshedToken = FIRInstanceID.instanceID().token() { 

     let userID = self.userData["id"] as! NSNumber 

     self.registerDeviceOnServerWith(refreshedToken, userID: userID) 
     print("InstanceID token: \(refreshedToken)") 
    } 

    // Connect to FCM since connection may have failed when attempted before having a token. 
    connectToFcm() 
} 

PS。私は、info.plistと同様に、機能からリモート通知チェックを設定しました。また、通知のペイロードから"content-available":1を追加して試してみました。

答えて

0

通知ペイロードに正しいスペルと文字の配置を記述する必要があります。 あなたの声明によると、コンテンツの利用可能な場所に間違った文字を配置した可能性があります。 あなたはそれがcontent_availbleあるべきcontent-availableを書かれている...この形式で

{ 
    "aps":{ 
     "alert":{ 
      "title":"Notification Title Text", 
      "Body" :"Notification Body", 
      "content_available":1 

     } 
    } 
} 

をあなたのペイロードを送ってください。これで、あなたのアプリを起動させて、ロジックをバックグラウンドでも実行できます。