我正在开发基于react-native的iO和Android应用程序,并使用aws pinpoint进行推送通知。我已经设法使用aws-amplify库获取通知以进行本机响应,并且当我使用aws-pinpoint测试工具测试通知时,它可以正常工作。但是我在使用php发送通知时遇到问题。
我尝试使用此文档(https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-pinpoint-2016-12-01.html#sendmessages),在Ios中,它显示以下错误:
{
"errorMessage": "Invalid notification",
"channelType": "APNS",
"pushProviderStatusCode": "0",
"pushProviderError": "Notification is malformed"
}
这是我正在尝试的PHP代码
$settings=array(
'version' => '2016-12-01', // have tries 'latest' too
'region' => 'us-east-1',
'credentials' => [
'key' => 'XXXXXXXXX',
'secret' => 'XXXXXX',
]
);
$pin = new Aws\Pinpoint\PinpointClient($settings);
$msgios=array(
'ApplicationId' => 'XXXXXXXX',
'MessageRequest' => [
'Addresses' => [
'06fdf172694c5d6461b3d8a20308720674XXXXXXXXX' => [
'BodyOverride' => 'aaa',
'ChannelType' => 'APNS',
'RawContent' => 'bbb',
'Context' => ['ccc' => '222'],
'TitleOverride' => 'ddd',
],
],
'Context' => ['hello'=>'yes', 'value'=>'key'],
'MessageConfiguration' => [
'APNSMessage' => [
'Action' => 'OPEN_APP',
'Badge' => 2,
'Body' => 'Hello There',
'Category' => 'iOS',
'CollapseId' => 'Yes',
'Data' => ['age'=>13,'Name'=>"Saman"],
'MediaUrl' => null,
'PreferredAuthenticationMethod' => '',
'Priority' => '10',
'RawContent' => 'Hello',
'SilentPush' => true,
'Sound' => 'default',
'Substitutions' => [
'ages' => ['10', '13'],
],
'ThreadId' => '10',
'TimeToLive' => 10,
'Title' => 'There',
'Url' => null,
],
'DefaultMessage' => [
'Body' => 'Hello there',
'Substitutions' => [
'ages' => ['10', '13'],
],
],
'DefaultPushNotificationMessage' => [
'Action' => 'OPEN_APP',
'Body' => 'Hello',
'Data' => ['age'=>13,'Name'=>"Saman"],
'SilentPush' => true,
'Substitutions' => [
'ages' => ['10', '13'],
],
'Title' => 'Hello',
'Url' => null,
],
],
'TraceId' => '1024585',
],
);
$result = $pin->sendMessages($msgios);
当我使用“ GCM”并将其推送到android时,我收到了来自iOs的错误消息,并通知到设备(我可以在控制台中看到它)但格式不正确。
iOS和Andoird通知可以通过AWS精确仪表板完美运行:
我认为我没有使用正确的语法或API版本。感谢您的帮助。
答案 0 :(得分:1)
在上面的PHP代码段中,我看到您正在使用'RawContent',并且被定义为普通字符串 例如'bbb' 和MessageRequest中的 'hello' 。但是,根据Amazon Pinpoint文档,在消息请求的“ AddressConfiguration” 或“ MessageConfiguration” 中使用”RawContent”时,需要将其定义/指定为JSON格式的字符串,因为这是用作通知消息的有效负载。您可以修改PHP脚本以包含“ RawContent” 参数,并为FCM / APNS正确设置其格式,如下所示:
require 'vendor/autoload.php';
use Aws\Pinpoint\PinpointClient;
use Aws\Exception\AwsException;
$settings=(array(
'credentials' => [
'key' => 'AKIAxxxxxxxxx',
'secret' => '+TsIDJvk0WVpZUXXXXXXXXXX',
],
'region' => 'us-east-1',
'version' => 'latest',
));
$pin = new Aws\Pinpoint\PinpointClient($settings);
$msgios = array(
'ApplicationId' => '4fd13a40bdXXXXXXXXX687c', // REQUIRED
'MessageRequest' => [ // REQUIRED
'Addresses' => [
'XXXXXXXXXXXXXXXX-qKEvbqpc' => [
//'BodyOverride' => 'aaa',
'ChannelType' => 'GCM',
'RawContent' => '{"notification":{"title":"PHP PUSH NOTIFICATION","body":"Hello, this is a test push notification!"}}', // If you define 'RawContent' here, everything ("message") in the "MessageConfiguration" will be ignored.
'Context' => ['ccc' => '222'],
'Substitutions' => [
'ages' => ['10', '13'],
],
//'TitleOverride' => 'ddd',
],
],
'Context' => ['hello'=>'yes', 'value'=>'key'],
'MessageConfiguration' => [ // REQUIRED
'DefaultMessage' => [
'Body' => 'Hello there',
'Substitutions' => [
'ages' => ['20', '23'],
],
],
'DefaultPushNotificationMessage' => [
'Action' => 'OPEN_APP',
'Body' => 'Hello World from Amazon Pinpoint!',
'Data' => ['age'=>33,'Name'=>"syumaK"],
'SilentPush' => false,
'Substitutions' => [
'ages' => ['30', '33'],
],
'Title' => 'PHP GCM PUSH NOTIFICATION',
'Url' => null,
],
'GCMMessage' => [
'Action' => 'OPEN_APP',
'Body' => 'This is a sample push notification sent from Amazon Pinpoint using AWS PHP SDK',
'Category' => 'Android',
'Data' => ['age'=>43,'Name'=>"syumaK"],
//'RawContent' => '{"notification":{"title":"PHP PUSH NOTIFICATION","body":"Hello, this is a test push notification!"}}', // If you uncomment this line, this will override everthing in this MessageConfiguration.
'SilentPush' => false,
'Sound' => 'default',
'Substitutions' => [
'ages' => ['40', '43'],
],
'Title' => 'GCM PUSH NOTIFICATION',
],
],
'TraceId' => '1024585',
],
);
$result = $pin->sendMessages($msgios);
print $result;
注意:
使用'RawContent'参数时,它将覆盖消息。您可以省略'DefaultMessage'和'DefaultPushNotificationMessage' > 配置,因为不使用它们。
另外,为“ RawContent” 构建有效负载时 参数,有效负载将需要包含“数据” 或 “通知” 键,否则会遇到以下错误消息: “ json消息中应包含数据或通知键”
如果不需要“ RawContent” 参数,则可以从FCM / APNS MessageRequest中完全忽略它。
摘要:
通常错误: “通知格式错误” 通常表示“消息请求”中提供的有效负载可能无效或格式不正确。因此,如果您决定在GCM频道或APNS频道中使用'RawContent'参数,则需要如上所述对字符串进行JSON格式化,否则可以省略'RawContent' ,就像上面在第一部分中所讨论的一样。
使用以下环境规范测试了以上代码段: