1

I am working on a iOs and Android Application built on top of react-native and using aws pinpoint for the push notifications. i have managed to get the notifications using aws-amplify library for react native and when i test notifications using aws-pinpoint test tool it works without a problem. however i am having problem with sending notifications using php.

I've tried using this documentation (https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-pinpoint-2016-12-01.html#sendmessages) and in Ios it gives following Error :

{
    "errorMessage": "Invalid notification",
    "channelType": "APNS",
    "pushProviderStatusCode": "0",
    "pushProviderError": "Notification is malformed"
}

Here is the PHP code i'm trying


    $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);

I get above error from iOs and when i Use 'GCM' and and push it to android notification is coming to the device (i can see it in the console) but not in the right format.

iOS and Andoird notifications works perfectly from AWS pinpoint dashboard :

screen
(source: kayantemp.com)

I think i'm not using the right syntax or the API version. appreciate your help.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
nicole ino
  • 33
  • 1
  • 9

1 Answers1

1

In your PHP code snippet above, I see you are using ’RawContent' and is defined as a normal string e.g ‘bbb' and ‘hello’ in the MessageRequest. However, according to Amazon Pinpoint docs when ”RawContent” is used either in ”AddressConfiguration” or ”MessageConfiguration” of the Message Request it needs to be defined/specified as a JSON-formatted string since this is the payload used as the notification message. You can modify your PHP script to include ”RawContent” parameter and format it correctly either for FCM/APNS as illustrated below :

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;

Note :

  1. When ‘RawContent' parameter is used, it overrides the message.You can omit both 'DefaultMessage' and 'DefaultPushNotificationMessage' configuration since they are not used.

  2. Additionally when constructing the payload for the ‘RawContent' parameter, the payload will need to contain either “data” or “notification” keys otherwise you run into the following error message : “data or notification key is expected in the json message"

In the event you don't need the ”RawContent” parameter, you can omit it entirely from your FCM/APNS MessageRequest.

Summary :

Usually the error : “Notification is malformed” usually indicates the payload provided in the “Message Request” is probably invalid or not formatted correctly. Therefore, if you decide to use ‘RawContent' parameter in either GCM channel or APNS channel then you will need to JSON-format the string as discussed above, otherwise you can omit the ‘RawContent' entirely as also discussed above in the first section.

Tested the above code snippet using the following environment spec:

  • Samsung Galaxy S6
  • iPhone 7
  • aws-sdk-php": "^3.108"
aksyuma
  • 2,957
  • 1
  • 15
  • 29