AWS Mobile Push with users that may be logged into multiple devices - amazon-web-services

Our apps are being developed for both Android and iOS. We are using AWS SNS Mobile Push to push messages to both GCM and APNS. The back end is PHP and so it uses the AWS PHP SDK.
Until now, our system has been saving 1 Endpoint ARN per user. We then broadcast a Mobile Push message directly to this Endpoint ARN.
The question:
It would be strange if a user were logged into our app on multiple devices and did not receive push notifications on all of them. So - we're going to have to change something.
How, in AWS Mobile Push, is the concept of 'one user, multiple devices (and potentially multiple platforms) handled?
Does our system have to maintain a one-to-many association of user-to-EndpointARNs (i.e. start saving multiple EndpointARNs per user so that we may push messages to all of them sequentially)?
Or should I be looking into the 'Topics' concept that Mobile Push provides (I'm confused here - do some people use one topic per user, and then push messages to the topic itself?)..
Lastly - I guess as a bonus question - is it normal for people using the PHP AWS SDK to include both APNS and GCM attributes in the Message payload array? We haven't been keeping track of 'which type of device each user uses'. We've just been saving an Endpoint ARN per user. And I guess the thought was to just cover all our platform bases (APNS, APNS_SANDBOX, GCM) when we're pushing a message to an endpoint.
I've been doing a lot of searching on the 'one user with multiple devices' topic re: Mobile Push with AWS SNS, and really the results have been quite unhelpful.
Help please :(
Thanks!

Once you send SNS Push message , you will get success or failure reason.
Could you please check your logs and share to identify exact issue.
Cloudwatch :
SNS publishes Cloudwatch metrics for number of messages published, number of successful notifications, number of failed notifications and size of data published. Metrics are available on per application basis. You can access Cloudwatch metrics via AWS Management Console or CloudWatch APIs.
Direct addressing:
Direct addressing allows you to deliver notifications directly to a single endpoint, rather than sending identical messages to all subscribers of a topic. This is useful if you want to deliver precisely targeted messages to each recipient. When you register device tokens with SNS, SNS creates an endpoint that corresponds to the token. You can publish to the token endpoint just as you would publish to a topic. You can direct publish either the text of your notification, or a platform-specific payload that takes advantage of platform-specific features such as updating the badge count of your app. Direct addressing is currently only available for push notifications endpoints.
Official documentation, "When a topic is created, Amazon SNS will assign a unique ARN (Amazon Resource Name) to the topic.
https://aws.amazon.com/sns/faqs/

I have a solution for this issue as you already know ARN is associated with single device token so you must be manage it own your own to send push to same user with multiple device.
My approach is to create 2 table in mysql or whatever database you are using
1. For APNS or, APNS_SANDBOX having details like user_id(local), device token, endpoint ARN, status etc (EG : sns_apns)
2. Same for GCM or BAIDU you have to create a table having user details with endpoint ARN(EG : sns_gcm)
Now every time when you send push to a particular user just write a code something like this
Switch($platform) {
case 'APNS':
case 'APNS_SANDBOX':
Select all users from sns_apns by login id.
Create a message and send it to all users having same user id
case 'GCM':
Select all users from sns_gcm by login id.
Create a message and send it to all users having same user id
}

I haven't tried this but to solve your problem I would keep a map of the user's cognito userID and corresponding endpoint ARN for each major app defining transaction done by the user. If the endpoint already exist then no need to save it. But if for this userID another endpoint ARN is notice then save, update, add or associate this new endpointARN with userID in DynamoDB as a #Document attribute. Then at anytime just broadcast to all endpointARN associated to the userID in DynamoDB.
The mysampleapp mobile hub example codes are useful for quick manipulation. For android:
Get the endpointARN in app like this:
PushManager pushManager;
String userDeviceEndPoint;
pushManager = AWSMobileClient.defaultMobileClient().getPushManager();
userDeviceEndPoint = pushManager.getEndpointArn();
You can update DynamoDB using the update save behaviour to ignore if an already existing endpoint is noticed for the user:
DynamoDBMapper mapper = new DynamoDBMapper(ddbClient, new DynamoDBMapperConfig(DynamoDBMapperConfig.SaveBehavior.UPDATE_SKIP_NULL_ATTRIBUTES));
Not sure how to do this in PHP though. Hope this idea helps.

Related

Google Pubsub Subscription based on particular email id

I am relatively new to GCP platform. I need to create a system wherein my team gets notifications whenever an email is received from there client.
I have to create a system which is publishing messages in pubsub topic filtered by email id.
example : I want to publish only those message/emails in which "To" and "From" fields has "example#gmail.com"
I have referred the online documentations but could not find the workaround.
Is this possible using GCP?
If not is there any other service via which I can achieve the same ?
We are using Gmail as the email client
Thanks
What you can use, is Pub/Sub together with Cloud Functions, which enables to access Gmail programmatically. Your specific scenario could look as following:
User sets up Gmail push notifications: every time a new message arrives at inbox, Gmail will send a notification to Cloud Pub/Sub.
Cloud Pub/Sub delivers the new message notification to Google Cloud Functions.
Upon arrival of the new message notification, a Cloud Functions instance connects to Gmail and retrieves the new message.
Check who send the message, and perform specific actions.
Before setting up a Cloud Function to automatically read your emails, you must authorize its access to Gmail. Have a look for codelab scenario and see step by step how to perform specific actions and adjust the function for your needs. Additionally, you can take a look for official documentation here.

How to send notifications to a specific user with AWS SNS?

I am developing an app like Dominoes. In which, I would like to send a Push notification to the Customer, when his/her Order is prepared.
I had been using OneSignal to do so (through sending notification to a particular player id), and now we would like to do it with AWS SNS Service, as we are using bunch of different AWS Services too.
We don't want any marketing/bulk push notifications, the SNS Service would do only one thing - send a particular message from the Restaurants' Mobile (using REST API), and it would reach the Customer's mobile.
With OneSignal, we used to give the PlayerID/UserID of the receiver. Does this method apply to AWS SNS too? Also, we could only work with HTTPS POST requests in our platform.
Any help is appreciated :)
Thanks!

AWS Mobile Push Notification by Example

I'm trying to wrap my head around how AWS Mobile Push Notification works. Specifically I'm building out a web service that will be capable of sending notifications to my mobile app running on my users' devices. After reading all of their docs, it sounds like the high level flow is:
Configuration
I need to go into my respective Push Notification Services (GCM for Android and APNS for iOS) and configure them to get credentials that I configure my backend service to use for connecting to them at runtime
I need to log in to the AWS SNS console and generated a platform application ARN (PlatformApplicationARN) that I also configure my backend to use
Code Flow (Runtime)
When a new user signs up for the first time, or anytime an existing user signs in on a new device, I have the app send my service their device info. One critical piece of this device info is their device token (also referred to as a "registration ID" in the AWS docs). This token is generated by their OS and uniquely identifies their device within their respective Push Notification Services (again either GCM or APNS for me)
When my service receives this new device info, I save it, and I also use the device token to hit the AWS SNS API (along with my configured PlatformApplicationARN) to generate a unique EndpointARN for that particular device
Now, whenever my backend decides it needs to send a notification to that user, I can look up all the devices associated with that user (that I previously stored in my DB), and fetch each device's EndpointARN. Then its just a matter of hitting the AWS SNS API to send my notification message to that EndpointARN, and it sounds like AWS SNS will take care of everything else (and delivering the actual message to the device)
So before I go any further, I'm just looking for someone to help sanity check my understanding and provide any course correction if I've misunderstood anything or am missing any important pieces of the config/flow! Assuming I'm more or less on track...
I'm still not seeing how SNS will be able to connect to GCM and APNS once I send a notification message to an EndpointARN. Do they maintain their own integration/connection with these services? Or do I somehow inject my own GCM/APNS credentials into the AWS SNS API call somehow?
Also, I know push notifications can be fairly configurable, allowing you to do things like:
Determine what sound the device should play when it receives a notification
Determine what color LED to blink on/off when it receives a notification (on my Android phone, different apps cause green, blue even purple LEDs to blink!)
Determine whether the notification is received by the Android/iOS OS itself (in which case if I come back to my phone after being away from it for a few minutes, I can press any button and see a high-level listed summary of any new notifications I've received); or whether the notification is purely an "in-app" notification in which case I'll only see that I received it if I actually open up my app.
I'm wondering where all this configuration takes place? Any ideas?
I can confirm that the high level flow is:
Log into AWS SNS and create 2 different Platform Applications, 1 for Android (FCM -- Firebase Cloud Messaging) and the other for iOS (APNS -- Apple Push Notification Services)
For each Platform Application you'll get a PlatformApplicationArn and you will be asked for credentials so that SNS can connect to your respective FCM/APNS accounts
For FCM you will just need your Server API Key (this can be obtained from Firebase Cloud Manager)
For APNS you will need to go through a really labor-intensive process of creating certificates through the Key Chain Access tool on your Mac, this was not fun...
Add these 2 PlaformApplicationArns to your code's config
When a user registers a new device with your app, they will send you a device token (provisioned by FCM or APNS) that uniquely identifies them to FCM/APNS
Take this device token, combined with your PlatformApplicationArn for FCM or APNS and use the AWS SNS SDK to create an EndpointArn for the device. Store this EndpointArn however you like.
Now you can use the AWS SNS SDK to push messages to your EndpointArn (specific device) anytime you want to.

Is there a way in AWS Simple Notification Services (SNS) to track who all unsubscribed?

We're using AWS SNS (Simple Notification Services) as one of the mechanisms of sending out notifications for users. That means, there are notifications sent through SMS (using AWS SNS) / Emails / automated calls.
But any time, if a user unsubscribes from any of these three methods, We should stop sending further alerts to the user in all the three modes.
Currently I don't have a way in API to check if a subscription request has been opted-out (by sending a STOP message), so that we can block the other two modes of communications as well.
Is it possible, in SNS? I've tried looking into the list of API, console and Amazon Forums.
I've figured out that this below behaviour of AWS SNS (for SMS end points) can be a workaround.
Although currently there is no way of getting a list of endpoints (users) who unsubscribe by responding STOP to 30304 (Amazon's short number.)
But, when we subscribe a user for a topic, AWS creates a subscription record with subscriptionArn = "PendingConfirmation"
If the user responds STOP (opts out), the subscription record is deleted from the topic. If the user subscribes to it, the subscription's subscriptionArn changes to some valid ARN (Amazon's resource Number) from "PendingConfirmation".
ListSubscriptionsByTopic API lists the user subscription even if it is in "PendingConfirmation" state.
So for now, only way to know if a user has unsubscribed from the topic would be, maintaining a list of users with their end points (phone numbers / email ids) and comparing it at later point of time with the list of available list of subscriptions. If something has disappeared, it means they've unsubscribed.
Of course, it can't be real-time. But this is the only workaround that is available for now.
As an additional info, if a subscription is pending for more than 72 hours, it is automatically deleted.

SNS Batch Publish

I see AWS publish API for sending push notifications to devices.
http://docs.aws.amazon.com/sns/latest/api/API_Publish.html
According to:
https://aws.amazon.com/blogs/aws/push-notifications-to-mobile-devices-using-amazon-sns/
We can
"Send messages directly to a specific device by calling the Publish function with the device’s ARN. You can easily scale this to handle millions of users by storing the endpoint ARNs in Amazon DynamoDB and using multi-threaded code on the server."
If I want to send push notifs to 100K users (who haven't registered to a specific topic), is there a multi-publish (or batch-publish) API, where I don't need to call the "Push notifications" API for every single user?
probably not. the devices need to be registered (i.e. for SNS you actually have to create the endpoint for each device).
After you have the endpoint you can subscribe them to either one or multiple SNS endpoint and start publishing notifications through them.