アプリがフォアグラウンドでは、通知には通知があるユーザーを特定する情報があると仮定します。現在ログインしているユーザを特定する情報をNSUserDefaults
に保存している場合は、didReceiveRemoteNotification
の情報を取得し、それに基づいて通知を処理するかどうかを決定する必要があります。
アップデート追加の詳細情報は:あなたは、あなたがしたいものから表示したいものを除外した後
また、その後、application:didReceiveRemoteNotification:fetchCompletionHandler を実装し、「サイレント」の通知にあなたの通知をオンにする必要があります。表示したいもののローカル通知を作成します。また、info.plistにUIBackgroundModesを追加し、ペイロードに「コンテンツ利用可能」キーを含める必要があります。詳細は、hereを参照してください。
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
</array>
それはアプリがバックグラウンドで動作しているときにはないものが呼び出されていないのでfetchCompletionHandlerとバージョンを実装するために重要です。
私のアプリケーションの1つで、ロケーションアップデートリクエストの「サイレント」通知を受け取り、「サイレントでない」通知を受け取る例です。アプリがアクティブな場合、私は「トースト」を表示します。アプリがバックグラウンドにある場合は、リモート通知からローカル通知を作成してトリガーします。上記の問題を解決するための
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
log?.info("******* got a remote message = \(userInfo) *******")
let message: String = (userInfo["message"] ?? "stop") as! String
log?.info("message = \(message)")
switch message {
case "update_locations":
log?.info("starting location update cycle")
locationUploader?.startUploading(handler)
case "micropost_created":
log?.info("got a new micropost notification")
let notification = UILocalNotification()
let userName = userInfo["user_name"] as? String ?? ""
let content = userInfo["content"] as? String ?? ""
notification.alertTitle = "Micropost from \(userName)"
let alertBody = "\(userName) said: \(content)"
notification.alertBody = alertBody
notification.soundName = UILocalNotificationDefaultSoundName
application.presentLocalNotificationNow(notification)
if let topMostView = application.keyWindow {
topMostView.makeToast(message: alertBody)
}
handler(.NewData)
default:
handler(.NoData)
}
}
出典
2016-05-24 22:51:50
nPn
はい、それは私はNSUserDefaultsにユーザ情報を保存します。しかし、 'didReceiveRemoteNotification'でAPN(スクリーン通知、バッジ、およびサウンドアラート)を処理するには、電話機をどう避けるのですか? 私はuseridに条件を置くことによって 'application(アプリケーション:UIApplication、didReceiveRemoteNotification userInfo:[NSObject:AnyObject]、fetchCompletionHandler completionHandler:(UIBackgroundFetchResult) - > Void)'で行いました.APNを扱うコードはうまくいきませんバックグラウンドでは、通知、バッジ、および警告音が表示されます。 – Thomi
申し訳ありませんが、あなたはfetchCompletionHandlerでバージョンを呼び出すように見えているようですが、あなたが紛失している部分は、APNペイロードのコンテンツ利用可能なキーかもしれません。 – nPn
私のペイロードには "content-available"が1に設定されていません: 'var note = new apn.Notification(); ... note.contentAvailable = 1; ' – Thomi