Table of Contents
Today, I want to share a process or steps to send push notifications to mobile applications from Salesforce using AWS Pinpoint service.
Here, I’m using AWS Service to send push notification because we have some limitations to send push notification from salesforce i.e., We need to create connected app to send push notification, and we can only send the push notification to the salesforce’s users, but I wanted to send the push notification to contacts instead of users.
So, I chose to use AWS Pinpoint Service as intermediate of Salesforce and mobile application.
Below are the steps to send push notification using Salesforce via AWS Pinpoint Service:
Step 1: Set up AWS Pinpoint service for Android and iOS.
- Go to the AWS Pinpoint service then select your project, if don’t have any project then create a new one.
- Now, go to the settings and enable the push notification for Android (GCM) and iOS (APNS & APNS Sandbox).
- To enable GCM, will need a JSON file, and that can be downloaded from the Firebase project. (Go to firebase console -> Select your project -> Go to project setting -> Go to service account -> Click on ‘Generate new private key’).
- To enable APNS/APNS Sandbox, will need a Team ID, Auth Key, bundle ID, and .p8 Certificate. (Auth Key, .p8 Certificate, and Team ID will be from the App Store Account)
Step 2: Now salesforce needs to create an Apex Class to write a push notification method to call AWS API in future calls to send push notifications.
- API: https://{{endpoint}}/v1/apps/{{pinpoint_project_Id}}/messages
- Where, the endpoint is “pinpoint.{aws_region}.amazonaws.com” and pinpoint_project_Id is also known as the application id of the pinpoint project.
- Payload:
{
"MessageConfiguration": {
"GCMMessage": {
"Body": "String",
"Title": "String",
"Action": "OPEN_APP",
"Data": {
" String ": "String"
}
},
"APNSMessage": {
"Body": " String ",
"Title": " String ",
"Action": "OPEN_APP",
"Data": {
" String ": " String "
}
},
"EmailMessage": {
"Body": " String ",
"FeedbackForwardingAddress": " String ",
"FromAddress": " String",
"SimpleEmail": {
"Subject": {
"Data": " String "
},
"HtmlPart": {
"Data": "String"
},
"TextPart": {
"Data": " String "
}
}
},
"SMSMessage": {
"Body": " String ",
"SenderId": " String ",
"MessageType": "TRANSACTIONAL",
"Keyword": " String ",
"OriginationNumber": " String "
}
},
"Addresses": {
"deviceToken || email || phoneNo": {
"ChannelType": "APNS || GCM || APNS_Sandbox || EMAIL || SMS"
}
}
}
- Apex Code:
public static String generateSignature(String method, String service, String host, String region, String endpoint, String requestParameters, String payload) {
String amzDate = Datetime.now().formatGMT('yyyyMMdd\'T\'HHmmss\'Z\'');
String dateStamp = Datetime.now().formatGMT('yyyyMMdd');
String canonicalUri = endpoint;
String canonicalQueryString = requestParameters;
String canonicalHeaders = 'host:' + host + '\n' + 'x-amz-date:' + amzDate + '\n';
String signedHeaders = 'host;x-amz-date';
String payloadHash = hash(payload);
String canonicalRequest = method + '\n' + canonicalUri + '\n' + canonicalQueryString + '\n' + canonicalHeaders + '\n' + signedHeaders + '\n' + payloadHash;
String algorithm = 'AWS4-HMAC-SHA256';
String credentialScope = dateStamp + '/' + region + '/' + service + '/aws4_request';
String stringToSign = algorithm + '\n' + amzDate + '\n' + credentialScope + '\n' + hash(canonicalRequest);
Blob signingKey = getSignatureKey(SECRET_KEY, dateStamp, region, service);
String signature = hmacSHA256(stringToSign, signingKey);
String authorizationHeader = algorithm + ' ' + 'Credential=' + ACCESS_KEY + '/' + credentialScope + ', ' + 'SignedHeaders=' + signedHeaders + ', ' + 'Signature=' + signature;
return authorizationHeader;
}
public static String hash(String data) {
Blob hash = Crypto.generateDigest('SHA-256', Blob.valueOf(data));
return EncodingUtil.convertToHex(hash).toLowerCase();
}
public static String hmacSHA256(String data, Blob key) {
Blob hmac = Crypto.generateMac('HmacSHA256', Blob.valueOf(data), key);
return EncodingUtil.convertToHex(hmac).toLowerCase();
}
public static Blob getSignatureKey(String key, String dateStamp, String regionName, String serviceName) {
Blob kSecret = Blob.valueOf('AWS4' + key);
Blob kDate = Crypto.generateMac('HmacSHA256', Blob.valueOf(dateStamp), kSecret);
Blob kRegion = Crypto.generateMac('HmacSHA256', Blob.valueOf(regionName), kDate);
Blob kService = Crypto.generateMac('HmacSHA256', Blob.valueOf(serviceName), kRegion);
Blob kSigning = Crypto.generateMac('HmacSHA256', Blob.valueOf('aws4_request'), kService);
return kSigning;
}
@future(callout = true)
public static void sendPushNotification(String message) {
try {
String method = 'POST';
String service = SERVICE_NAME;
String host = HOST;
String region = REGION;
String endpoint = '/v1/apps/' + APP_ID + '/messages';
String requestParameters = '';
String payload = message;
String authorizationHeader = generateSignature(method, service, host, region, endpoint, requestParameters, payload);
HttpRequest req = new HttpRequest();
req.setEndpoint('https://' + host + endpoint + '?' + requestParameters);
req.setMethod(method);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('x-amz-date', Datetime.now().formatGMT('yyyyMMdd\'T\'HHmmss\'Z\''));
req.setHeader('Content-Type', 'application/json');
req.setBody(payload);
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getStatusCode());
System.debug(res.getStatus());
System.debug(res.getBody());
} catch (Exception e) {
System.debug(e.getCause());
System.debug(e.getMessage());
System.debug(e.getLineNumber());
System.debug(e.getStackTraceString());
}
}
Create a new method for payload and pass that payload as parameter in ‘sendPushNotification’ method. Then you can trigger this method from any Apex Trigger or any Apex Class.
Step 3: Set up in Android and iOS to receive push notifications.
- Follow the steps to set up in Android using the below URL.
- https://docs.amplify.aws/gen1/android/build-a-backend/push-notifications/set-up-push-notifications/
- Follow the steps to set up in iOS using the below URL.
- https://docs.amplify.aws/gen1/swift/build-a-backend/push-notifications/set-up-push-notifications/
Conclusion
This article presents a step-by-step guide for sending push notifications from Salesforce to mobile applications using AWS Pinpoint. By leveraging AWS Pinpoint, the process overcomes Salesforce’s inherent limitation of only notifying users and extends the capability to contacts.
It involves setting up AWS Pinpoint for mobile platforms, creating an Apex class in Salesforce to handle API calls, and ensuring mobile devices are configured to receive these notifications.
This approach effectively bridges Salesforce with mobile communication channels, allowing for broader and more flexible push notification strategies.
So, this is the process to send push notifications from Salesforce to Mobile Applications via AWS Pinpoint service.
I hope this article will be helpful for someone.