本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
AWS 最終使用者傳訊推送 API 可以將交易推送通知傳送至特定裝置識別符。本節包含完整的程式碼範例,您可以使用 AWS SDK,透過 AWS 最終使用者傳訊推送 API 傳送推送通知。
您可以使用這些範例,透過 AWS 最終使用者訊息推送支援的任何推送通知服務傳送推送通知。目前, AWS 最終使用者傳訊推送支援下列頻道:Firebase Cloud Messaging (FCM)、Apple Push Notification Service (APNs)、百度雲端推送和 Amazon Device Messaging (ADM)。
如需端點、區段和頻道的更多程式碼範例,請參閱程式碼範例。
注意
當您透過 Firebase Cloud Messaging (FCM) 服務傳送推送通知時,請在呼叫 AWS 最終使用者訊息推送 API GCM
時使用服務名稱。Google 已於 2018 年 4 月 10 日終止 Google Cloud Messaging (GCM) 服務。不過, AWS 最終使用者傳訊推送 API 會使用其透過 FCM GCM
服務傳送之訊息的服務名稱,以維持與終止 GCM 服務之前寫入之 API 程式碼的相容性。
下列範例使用 send-messages 傳送 GCM Push 通知與 AWS CLI。將字符
取代為裝置的唯一字符,並將 611e3e3cdd47474c9c1399a50example
取代為您的應用程式識別符。
aws pinpoint send-messages \
--application-id 611e3e3cdd47474c9c1399a50example
\
--message-request file://myfile.json \
--region us-west-2
Contents of myfile.json:
{
"Addresses": {
"token
": {
"ChannelType" : 'GCM'
}
},
"MessageConfiguration": {
"GCMMessage": {
"Action": "URL",
"Body": "This is a sample message",
"Priority": "normal",
"SilentPush": True,
"Title": "My sample message",
"TimeToLive": 30,
"Url": "https://www.example.com"
}
}
}
下列範例使用 send-messages 傳送 GCM Push 通知,並使用所有舊版金鑰搭配 AWS CLI。將字符
取代為裝置的唯一字符,並將 611e3e3cdd47474c9c1399a50example
取代為您的應用程式識別符。
aws pinpoint send-messages \
--application-id 611e3e3cdd47474c9c1399a50example
\
--message-request
'{
"MessageConfiguration": {
"GCMMessage":{
"RawContent": "{\"notification\": {\n \"title\": \"string\",\n \"body\": \"string\",\n \"android_channel_id\": \"string\",\n \"body_loc_args\": [\n \"string\"\n ],\n \"body_loc_key\": \"string\",\n \"click_action\": \"string\",\n \"color\": \"string\",\n \"icon\": \"string\",\n \"sound\": \"string\",\n \"tag\": \"string\",\n \"title_loc_args\": [\n \"string\"\n ],\n \"title_loc_key\": \"string\"\n },\"data\":{\"message\":\"hello in data\"} }",
"TimeToLive" : 309744
}
},
"Addresses": {
"token
": {
"ChannelType":"GCM"
}
}
}'
\ --region us-east-1
下列範例使用 send-messages,使用 傳送具有 FCMv1 訊息承載的 GCM Push 通知 AWS CLI。將字符
取代為裝置的唯一字符,並將 611e3e3cdd47474c9c1399a50example
取代為您的應用程式識別符。
aws pinpoint send-messages \
--application-id 6a2dafd84bec449ea75fb773f4c41fa1 \
--message-request
'{
"MessageConfiguration": {
"GCMMessage":{
"RawContent": "{\n \"fcmV1Message\": \n {\n \"message\" :{\n \"notification\": {\n \"title\": \"string\",\n \"body\": \"string\"\n },\n \"android\": {\n \"priority\": \"high\",\n \"notification\": {\n \"title\": \"string\",\n \"body\": \"string\",\n \"icon\": \"string\",\n \"color\": \"string\",\n \"sound\": \"string\",\n \"tag\": \"string\",\n \"click_action\": \"string\",\n \"body_loc_key\": \"string\",\n \"body_loc_args\": [\n \"string\"\n ],\n \"title_loc_key\": \"string\",\n \"title_loc_args\": [\n \"string\"\n ],\n \"channel_id\": \"string\",\n \"ticker\": \"string\",\n \"sticky\": true,\n \"event_time\": \"2024-02-06T22:11:55Z\",\n \"local_only\": true,\n \"notification_priority\": \"PRIORITY_UNSPECIFIED\",\n \"default_sound\": false,\n \"default_vibrate_timings\": true,\n \"default_light_settings\": false,\n \"vibrate_timings\": [\n \"22s\"\n ],\n \"visibility\": \"VISIBILITY_UNSPECIFIED\",\n \"notification_count\": 5,\n \"light_settings\": {\n \"color\": {\n \"red\": 1,\n \"green\": 2,\n \"blue\": 3,\n \"alpha\": 6\n },\n \"light_on_duration\": \"112s\",\n \"light_off_duration\": \"1123s\"\n },\n \"image\": \"string\"\n },\n \"data\": {\n \"dataKey1\": \"priority message\",\n \"data_key_3\": \"priority message\",\n \"dataKey2\": \"priority message\",\n \"data_key_5\": \"priority message\"\n },\n \"ttl\": \"10023.32s\"\n },\n \"apns\": {\n \"payload\": {\n \"aps\": {\n \"alert\": {\n \"subtitle\": \"string\",\n \"title-loc-args\": [\n \"string\"\n ],\n \"title-loc-key\": \"string\",\n \"launch-image\": \"string\",\n \"subtitle-loc-key\": \"string\",\n \"subtitle-loc-args\": [\n \"string\"\n ],\n \"loc-args\": [\n \"string\"\n ],\n \"loc-key\": \"string\",\n \"title\": \"string\",\n \"body\": \"string\"\n },\n \"thread-id\": \"string\",\n \"category\": \"string\",\n \"content-available\": 1,\n \"mutable-content\": 1,\n \"target-content-id\": \"string\",\n \"interruption-level\": \"string\",\n \"relevance-score\": 25,\n \"filter-criteria\": \"string\",\n \"stale-date\": 6483,\n \"content-state\": {},\n \"timestamp\": 673634,\n \"dismissal-date\": 4,\n \"attributes-type\": \"string\",\n \"attributes\": {},\n \"sound\": \"string\",\n \"badge\": 5\n }\n }\n },\n \"webpush\": {\n \"notification\": {\n \"permission\": \"granted\",\n \"maxActions\": 2,\n \"actions\": [\n \"title\"\n ],\n \"badge\": \"URL\",\n \"body\": \"Hello\",\n \"data\": {\n \"hello\": \"hey\"\n },\n \"dir\": \"auto\",\n \"icon\": \"icon\",\n \"image\": \"image\",\n \"lang\": \"string\",\n \"renotify\": false,\n \"requireInteraction\": true,\n \"silent\": false,\n \"tag\": \"tag\",\n \"timestamp\": 1707259524964,\n \"title\": \"hello\",\n \"vibrate\": [\n 100,\n 200,\n 300\n ]\n },\n \"data\": {\n \"data1\": \"priority message\",\n \"data2\": \"priority message\",\n \"data12\": \"priority message\",\n \"data3\": \"priority message\"\n }\n },\n \"data\": {\n \"data7\": \"priority message\",\n \"data5\": \"priority message\",\n \"data8\": \"priority message\",\n \"data9\": \"priority message\"\n }\n }\n \n}\n}",
"TimeToLive" : 309744
}
},
"Addresses": {
token
: {
"ChannelType":"GCM"
}
}
}'
\ --region us-east-1
如果將 ImageUrl
欄位用於 GCM ,則 pinpoint 會將 欄位作為資料通知傳送,金鑰為 pinpoint.notification.imageUrl
,這可防止影像從框中轉譯出來。請使用 RawContent 或新增資料金鑰的處理,例如整合您的應用程式 AWS Amplify。
其他資源
-
如需推送管道範本的詳細資訊,請參閱《Amazon Pinpoint 使用者指南》中的建立推送通知範本。