1.准备工作
首先,您需要先安装AWS SDK并在代码中创建一个AWS Pinpoint客户端。 接下来,您需要准备您的设备令牌或端点ID以及您的AWS Pinpoint项目ID。
2.创建推送消息
接下来,使用您的AWS Pinpoint客户端创建一个推送消息。 这里我们使用AWS SDK for Node.js作为示例。
const AWS = require('aws-sdk');
const pinpoint = new AWS.Pinpoint();
const appId = 'YOUR_APP_ID';
const message1 = {
MessageRequest: {
Addresses: {
// Replace the empty string with the recipient's device token or endpoint ID.
'': {
ChannelType: 'APNS',
},
},
MessageConfiguration: {
APNSMessage: {
Action: 'OPEN_APP',
Body: 'Hello, World!',
Sound: 'default',
},
// Customize the optional metadata that's associated with the message.
// Here, we add a key-value pair that specifies the dispatch time
// as a timestamp (in ISO 8601 format).
CustomMessageData: {
dispatchTime: new Date().toISOString(),
},
},
},
};
3.添加更多的推送消息
您可以为每个设备令牌或端点ID添加一个推送消息,或者您可以为所有设备添加一个相同的推送消息。 然后,将所有推送消息添加到消息数组中。
const message2 = {
MessageRequest: {
Addresses: {
// Replace the empty string with the recipient's device token or endpoint ID.
'': {
ChannelType: 'APNS',
},
},
MessageConfiguration: {
APNSMessage: {
Action: 'OPEN_APP',
Body: 'Goodbye, World!',
Sound: 'default',
},
},
},
};
const messages = [message1, message2];
4.使用消息数组发送推送通知
现在,使用Aws Pinpoint的sendMessages方法将消息数组发送给设