Howdy!
Is is possible to send actual Push Notifications with localstack via FCM and APNS when the PlatformApplications
are created with real FCM and APNS credentials?
Here the sequence on commands we’ve been trying with real APNS keys and deviceIDs
~ > localstack --version
3.0.2
script:
import {
CreatePlatformApplicationCommand,
CreatePlatformEndpointCommand,
CreateTopicCommand,
PublishCommand,
SNSClient,
SubscribeCommand,
} from '@aws-sdk/client-sns';
import { uuid } from '../src/tests/helpersTS';
/**
* HOW TO
*
* run:
* npx ts-node push-notifications.ts
*
* retrieve messages:
* curl "http://localhost:4566/_aws/sns/platform-endpoint-messages" | jq .
*
*/
const apnKey = `-----BEGIN PRIVATE KEY-----
testapnKey
-----END PRIVATE KEY-----`;
const apnKeyId = 'testApnKeyId';
const run = async () => {
const client = new SNSClient({
apiVersion: '2010-12-01',
credentials: {
accessKeyId: 'test',
secretAccessKey: 'test',
},
endpoint: 'http://127.0.0.1:4566',
region: 'us-east-1',
});
/* Create Platform Application */
const createApplicationResponse = await client.send(
new CreatePlatformApplicationCommand({
Attributes: {
PlatformCredential: apnKey,
PlatformPrincipal: apnKeyId,
},
Name: 'Test Push Notifications',
Platform: 'APNS',
}),
);
console.log('Platform Application', { createApplicationResponse });
const platformArn = createApplicationResponse.PlatformApplicationArn;
/* Create Platform Endpoint */
const deviceId = uuid(); // would be a real deviceId
const createEndpointResponse = await client.send(
new CreatePlatformEndpointCommand({
PlatformApplicationArn: platformArn,
Token: deviceId,
}),
);
console.log('Platform Endpoint', { createEndpointResponse });
const endpointArn = createEndpointResponse.EndpointArn;
/* Create SNS Topic */
const createTopicResponse = await client.send(
new CreateTopicCommand({
DataProtectionPolicy: 'STRING_VALUE',
Name: 'PushNotificationTestTopic',
}),
);
console.log('Create Topic Response', { createTopicResponse });
const topicArn = createTopicResponse.TopicArn;
/* Subscribe 1st Endpoint to Topic */
const subscribeResponse = await client.send(
new SubscribeCommand({
Attributes: {
FilterPolicy: JSON.stringify({
deviceIds: [deviceId],
}),
},
Endpoint: endpointArn,
Protocol: 'application',
ReturnSubscriptionArn: true,
TopicArn: topicArn,
}),
);
console.log('subscribe Response', { suscribeResponse: subscribeResponse });
const subscriptionArn = subscribeResponse.SubscriptionArn;
/* Publish Message to 1st Device Id */
const publishResponse1 = await client.send(
new PublishCommand({
Message: `This is a test for 1st device ${new Date().toISOString()}`,
MessageAttributes: {
deviceIds: {
DataType: 'String.Array',
StringValue: JSON.stringify([deviceId]),
},
},
TopicArn: topicArn,
}),
);
console.log('Publish Response to deviceId1', { publishResponse1 });
};
run();