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.
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.
{
"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"
}
}
}
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.
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.