を実行:スウィフト - 私は通知を受け取ったときに、バックグラウンドでこのコードを実行しようとしているバックグラウンドタスク
window?.rootViewController?.dismissViewControllerAnimated(false, completion: nil)
NSNotificationCenter.defaultCenter().postNotificationName("ResetStatusBar", object: nil)
だから私はそうのようなdidReceiveRemoteNotification
にこのコードを配置した:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("Remote notification received in didReceiveRemoteNotification with fetchCompletionHandler")
// just pop to rootViewController
window?.rootViewController?.dismissViewControllerAnimated(false, completion: nil)
NSNotificationCenter.defaultCenter().postNotificationName("ResetStatusBar", object: nil)
}
このコードは、アプリがアクティブなときに通知を受け取ったとき、または受信した通知をユーザがクリックしたときに実行されますが、通知を受け取ったら無視してアプリを開きます(私は、ちょうどホームボタンを押して)、その変更はアプリに反映されていない結婚。
は、ここで私が現在やってる代以下のとおりです。
- が
remote notifications
は、バックグラウンドモードのために機能の下で点検したプッシュ通知ペイロードに'content-available': 1
を送信
これは私がどのようにもありますAppDelegateファイルにプッシュ通知を登録する:
let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert,.Badge,.Sound], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
UIApplication.sharedApplication().registerForRemoteNotifications()
私のコードが実行されていない理由は何ですか?それはUIの変更だからですか?
P.S.私はaps
オブジェクト内content-available
キーを置くことで修正
let payload = {
default: notificationDisplayText,
APNS: {
aps: {
alert: notificationDisplayText,
sound: 'default',
badge: 1,
'content-available' : 1
},
notificationId: notificationId
}
}
アム、または私はAPNS
オブジェクト内で直接それを入れ子にする必要があります。ここに私のプッシュ通知ペイロードは次のようになりますか?
ありがとうたくさんの男。だから要約すると、didReceiveRemoteNotificationは、アプリがバックグラウンドにある場合、正しいでしょうか?また、アプリが実行されていない、つまり中断されていない場合、通知はlaunchOptions辞書に追加されますか?最後にdidReceiveRemoteNotificationのcompletion handlerをcompletionHandler()と呼びますか?または、データを渡すはずですか? – Thomas
ああ、私は.NewDataまたは.NoDataを渡します。 – Thomas