Search code examples
node.jsamazon-web-servicesaws-sdkaws-sdk-jsaws-pinpoint

AWS Pinpoint Push Notification for NodeJS has no sound


Currently I have this parameter to create a message in pinpoint,

{
    ApplicationId: config.PROJECT_ID,
    MessageRequest: {
        Addresses: {
            [token]: {
                ChannelType: 'APNS'
            }
        },
        MessageConfiguration: {
            APNSMessage: {
                Title: notification.title,
                Body: notification.message
            }
        }
    }
}

I based my code here https://docs.aws.amazon.com/pinpoint/latest/developerguide/send-messages-push.html, I checked it using postman and there's no sound when push notification appear, I noticed that there's no parameter available for sound, how can I add a sound in my notification?

In AWS Pinpoint Test Messaging I used this parameter and it's working as expected, but when I tried it and applied it using the above code, it says "Unexpected key 'aps' found in params.MessageRequest.MessageConfiguration"

{
    APNSMessage: {
        aps: {
            alert: {
                title: notification.title,
                body: notification.message
            },
            sound: 'default'
        }
    }
}

I need to add a sound with the value of default using aws-sdk in NodeJS


Solution

  • Amazon Pinpoint SendMessages API doesn't expect/allow the "aps" dictionary to be specified when sending the push notification using a standard message.

    • To send push notification with sound using standard message, kindly refer to the following working sample code snippet :

      // Specify the parameters to pass to the API.
      var params = {
        ApplicationId: '4fd13a40xxxxxxxx',
        MessageRequest: {
          Addresses: {
            ["token from APNS"]: {
              ChannelType: 'APNS'
            }
          },
          MessageConfiguration: {
              APNSMessage: {
              Title: 'TEST',
              Body: 'This is a sample push notification sent from Amazon Pinpoint by using Nodejs SDK',
              Sound: 'default',
            }
          }
        }
      };
      
    • To send push notification with sound using the "aps" dictionary, then you'll need to use :

    ( i ) RawContent property: If using Pinpoint SDK/REST API/CLI.The RawContent property needs to be defined/specified as a JSON-formatted string as illustrated below :

    // Specify the parameters to pass to the API.
    var params = {
      ApplicationId: '4fd13a40xxxxxxxx',
      MessageRequest: {
        Addresses: {
          "token from APNS": {
            ChannelType: 'APNS'
          }
        },
        MessageConfiguration: {
            APNSMessage: {
            RawContent : '{"aps":{"alert":"Hello, this is a test push notification!","sound":"default"}}' // If you define 'RawContent' here, everything ("message") else in the "MessageConfiguration" will be ignored.
    
          }
        }
      }
    };
    

    ( ii ) RawMessage property : If using Pinpoint console as illustrated below :

    {
        APNSMessage: {
            aps: {
                alert: {
                    title: notification.title,
                    body: notification.message
                },
                sound: 'default'
            }
        }
    }
    

    this explains why, when you tested in AWS Pinpoint console and selected Test Messaging & Raw Message then the notification worked correctly with sound.