プッシュ通知に関連付ける必要のあるメタデータがいくつかあります。
たとえば、ユーザー番号、メッセージID。
アップルがサポートするよりも多くのパラメータを送信できますか?
{aps = {
alert = joetheman;
sound = default;
};}
これは可能ですか?
プッシュ通知に関連付ける必要のあるメタデータがいくつかあります。
たとえば、ユーザー番号、メッセージID。
アップルがサポートするよりも多くのパラメータを送信できますか?
{aps = {
alert = joetheman;
sound = default;
};}
これは可能ですか?
はい。プッシュ通知プログラミングガイドセクションの通知ペイロードには、
プロバイダーは、Appleが予約したaps名前空間の外部でカスタムペイロード値を指定できます。カスタム値は、JSON構造化およびプリミティブ型(ディクショナリ(オブジェクト)、配列、文字列、数値、およびブール)を使用する必要があります。カスタムペイロードデータとして顧客情報を含めないでください。代わりに、コンテキスト(ユーザーインターフェイス用)や内部メトリックの設定などの目的で使用してください。たとえば、カスタムペイロード値は、インスタントメッセージクライアントアプリケーションで使用するための会話識別子、またはプロバイダーが通知を送信した日時を識別するタイムスタンプである場合があります。アラートメッセージに関連するアクションは、破壊的であってはなりません。たとえば、デバイス上のデータを削除するなどです。
したがって、ペイロードは次のようになります
{
"aps": {
"alert": "joetheman",
"sound": "default"
},
"message": "Some custom message for your app",
"id": 1234
}
その同じページのさらに下には、これを示すいくつかの例があります。
もちろん。アップルプッシュ通知を使用して、カスタムパラメータをペイロードとして送信できます。Kevin Ballardが言ったように、ペイロードは上記のようになります。ただし、常にプッシュ通知を処理していることを覚えておいてください。アップルの制約に従って、プッシュ通知を処理します。通知ペイロードに許可される最大サイズは256バイトです。Apple Push Notification Serviceは、これを超える通知を拒否します。したがって、通知にデータを追加する場合は、これも考慮に入れてください。
apsタグ内にカスタムタグを配置することは許可されていません。ドキュメントには次のように書かれています。
Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean.
したがって、あなたの場合、次のようなことをする必要があります。
{
"aps": {
"alert": "Hello Push",
"sound": "default"
},
"People": {
"Address": "Your address here",
"Name": "Your Name here",
"Number": "XXXXXXXXXX"
}
}
したがって、「aps」ではなく、メインのJSONでキーを探してカスタムペイロードを読み取ることができます。
このチュートリアルに基づいてカスタムキー/値を送信する方法は次のとおりです
func sendPushNotification(to token: String, title: String, body: String, messageId: String, fromUsername: String, fromUID: String, timeStamp: Double) {
let urlString = "https://fcm.googleapis.com/fcm/send"
let url = NSURL(string: urlString)!
// apple's k/v
var apsDict = [String: Any]()
apsDict.updateValue(title, forKey: "title")
apsDict.updateValue(body, forKey: "body")
apsDict.updateValue(1, forKey: "badge")
apsDict.updateValue("default", forKey: "sound")
// my custom k/v
var myCustomDataDict = [String: Any]()
myCustomDataDict.updateValue(messageId, forKey: "messageId")
myCustomDataDict.updateValue(fromUsername, forKey: "username")
myCustomDataDict.updateValue(fromUID, forKey: "uid")
myCustomDataDict.updateValue(timeStamp, forKey: "timeStamp")
// everything above to get posted
var paramDict = [String: Any]()
paramDict.updateValue(token, forKey: "to")
paramDict.updateValue(apsDict, forKey: "notification")
paramDict.updateValue(myCustomDataDict, forKey: "data")
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
// ** add the paramDict here **
request.httpBody = try? JSONSerialization.data(withJSONObject: paramDict, options: [.prettyPrinted])
// send post ...
}
AppDelegateの通知からすべてを読み取る方法は次のとおりです。以下のメソッドは、AppleDelegateのデリゲートメソッドです。このチュートリアルを使用してください
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void {
let userInfo = response.notification.request.content.userInfo
if let userInfo = userInfo as? [String: Any] {
readPushNotification(userInfo: userInfo)
}
completionHandler()
}
func readPushNotification(userInfo: [String: Any]) {
for (k,v) in userInfo {
print(k)
print(v)
}
// my custom k/v
if let messageId = userInfo["messageId"] as? String {
print(messageId)
}
if let fromUsername = userInfo["username"] as? String {
print(fromUsername)
}
if let fromUID = userInfo["uid"] as? String {
print(fromUID)
}
if let timeStamp = userInfo["timeStamp"] as? String {
print(timeStamp)
}
// apple's k/v
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? [String: Any] {
if let body = alert["body"] as? String {
print(body)
}
if let title = alert["title"] as? String {
print(title)
}
}
if let badge = aps["badge"] as? Int {
print(badge)
}
}
}