I need to send SNS notification from an Android mobile device targeting another specific Android device endpoint. I could do this last year with the code below. Now it just doesn't work any more. Does anyone have similar issues with sending SNS notification to specific mobile endpoint and how can it be resolved?
AmazonSNSClient snsClient = new AmazonSNSClient(identityManager.getCredentialsProvider());
PublishRequest publishRequest = new PublishRequest();
publishRequest.setMessage(message);
publishRequest.setSubject(subject);
publishRequest.withTargetArn(mobilePhoneEndpointARN);
snsClient.publish(publishRequest);
I have resolved the problem. I added a custom "sns:Publish" permission to my authenticated mobile user policy group.
-Problem solved-
Related
I'm making an Android app in which I want user to sign in to their Outlook account and receive push notifications to the app from the Microsoft Graph API when an email is received in their inbox. How can I do this?
I can subscribe to inbox changes using a HTTP subscription request (as specified here https://learn.microsoft.com/en-us/graph/webhooks?view=graph-rest-1.0), with something like:
POST https://graph.microsoft.com/v1.0/subscriptions
Content-Type: application/json
{
"changeType": "updated",
"notificationUrl":
"https://webhook.azurewebsites.net/notificationClient",
"resource": "/me/mailfolders('inbox')/messages",
"expirationDateTime": "2016-03-20T11:00:00.0000000Z",
"clientState": "SecretClientState"
}
In this request I need to specify a "notificationUrl" where notification updates are sent to - how can I set this up? Is there functionality for this on Azure?
From there I believe I can use the instructions here to send push notifications to the Android device https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-android-push-notification-google-fcm-get-started.
This involves setting up a notification hub on Azure which connects to Firebase, which then sends notifications the app. Is this the best/only way to do this?
Any help much appreciated!
The notificationUrl can be the webhook url of the azure function app. https://azure.microsoft.com/en-us/resources/videos/create-a-web-hook-or-api-azure-function/.
Therefore, you can use azure function to invoke Firebase API to send notifications.
Please see https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-notification-hubs#packages---functions-1x and https://firebase.google.com/docs/cloud-messaging/migrate-v1.
Besides, I would recommend you to use azure logic app. It has built in connector to use when a message arrives your inbox.
Take a look here https://learn.microsoft.com/en-us/azure/connectors/connectors-create-api-office365-outlook.
I need to send push notifications between Android devices for an AWS Mobile Hub project. It is only device-to-device messaging, no topics involved. I have already integrated FCM and PinPoint
pinpointManager.getNotificationClient().registerDeviceToken(newToken)
I'm getting the endPointID using this code
String epID = pinpointManager.getTargetingClient().currentEndpoint().getEndpointId();
I can successfully push messages to the device from PinPoint console using the above endPointID. But I want to push from the android device itself. However, since there isn't any publish API available in PinPoint Android SDK, I have integrated SNS. I'm trying to publish with SNS using the endpointID received from PinPoint using this code:
PublishRequest publishRequest = new PublishRequest();
publishRequest.setTargetArn(epID);
publishRequest.setMessage("Hello from android");
AmazonSNSClient snsClient = new AmazonSNSClient(App.getCCCProvider());
snsClient.publish(publishRequest);
but receiving the following exception
com.amazonaws.services.sns.model.InvalidParameterException: Invalid parameter: TargetArn Reason: An ARN must have at least 6 elements, not 1 (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: 7ff39768-c6f9-5a6e-8211-c5ec586276fb)
If it helps, my endpointID is : 1fa93529-a5ac-4d70-995a-be1584c68a37
Any pointers or solutions from you guys?
The API you are looking for is the Pinpoint.sendMessages API.
REST Documentation
JavaDoc for sendMessages
Your request would probably look something like:
amazonPinpoint.sendMessages(
new SendMessagesRequest()
.withApplicationId("APP_ID")
.withMessageRequest(
new MessageRequest()
.withMessageConfiguration(
new DirectMessageConfiguration()
.withGCMMessage(
new GCMMessage()
.withBody("Hello from android")
)
)
.addEndpointsEntry(
"DESTINATION_ENDPOINT_ID",
// You can provide overrides and the like here
new EndpointSendConfiguration()
)
)
)
I am migrating from custom Push Notification to AWS SNS. I did the following.
configured AWS SNS Topic
Created Platform Application
created Endpoints
Suscribed endpoints to Platform Application
I am trying the last step for Pushing the Notification using 'publish' API for Ruby. I receive the message_id as response. It also logs as success / delivered in CloudWatch. But the Push is not received in the device.
The device is valid
The device has a valid push token
the device has the application installed.
The message format I tried areas follows
{"GCM"=>{"data"=>"{\"message\":\"message\",\"url\":\"url\"}"}, :default=>"default"}
SNS publish API syntax
client.publish({target_arn: endpoint_arn, message: message.to_json, subject: "SNS test", message_structure: :json})
FYI, Before marking this as duplicate of any other question, please note that i have referred all previous answers and nothing helped me. hence I am raising a new one.
The GCM payload format is updated.
This works for me.
{
"GCM": "{ \"notification\": { \"body\": \"hello....\", \"title\": \"title123\" } }"
}
There is a problem with my message formation. I figured it out by debugging with android . The Push reached the device but it was not in the expected format. So the push was not shown in the device. I was sending
{"GCM"=>{"data"=>"{\"message\":\"message\",\"url\":\"url\"}"}, :default=>"default"}
But the Android system was expecting 'badge' and some other keys in my message. I tried with
{:default=>"default", :GCM=>"{\"data\":{\"message\":\"test\",\"url\":\"https://s3.amazonaws.com/mcds_fulltext/aapg/gsaapgbull/issues/74/android_hdpi/cover_74.gif\",\"registration_ids\":\"arn:aws:sns:us-****-1:*********7:endpoint/GCM/gs*****_gcm/*********************\",\"badge\":\"10\",\"vol\":\"128\",\"issue\":\"10\",\"journal_name\":\"gsaapg\",\"abbr\":\"aapg\"},\"collapse_key\":\"New Article badge\"}"}
Now it works as expected.
This particular JSON might not be appropriate for everyone. But the cause of this issue is the message JSON is not in the expected format for the mobile app. This might be helpful for someone who face the similar issue.
I am following the Azure Mobile Services e-book for setting up Push Notifications:
https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter5/android/#registering-for-push-notifications
But am having problems with registering with a Tag:
var registrationId = GcmClient.GetRegistrationId(RootView);
//var push = client.GetPush();
//await push.RegisterAsync(registrationId);
var installation = new DeviceInstallation
{
InstallationId = client.InstallationId,
Platform = "gcm",
PushChannel = registrationId
};
// Set up tags to request
installation.Tags.Add("topic:Sports");
// Set up templates to request
PushTemplate genericTemplate = new PushTemplate
{
Body = #"{""data"":{""message"":""$(message)""}}"
};
installation.Templates.Add("genericTemplate", genericTemplate);
// Register with NH
var response = await client.InvokeApiAsync<DeviceInstallation, DeviceInstallation>(
$"/push/installations/{client.InstallationId}",
installation,
HttpMethod.Put,
new Dictionary<string, string>());
I have ensured that the tag is listed in the Azure Portal as a "Client Requested" tag, but still my registrations appear without the tag in the Device Registrations:
Any ideas what I am doing wrong?
It would appear that the e-book is out of date and is dealing with the old version of Mobile Services instead of Azure App Service Mobile Apps (seriously, how is anyone supposed to google around for the different versions when they are named so similarly?!)
https://github.com/Azure-Samples/app-service-mobile-dotnet-backend-quickstart/blob/master/README.md#push-to-users
(Emphasis is mine)
When a mobile app registers for push notifications using an Azure App Service Mobile Apps backend, there are two default tags that can get added to the registration in Azure Notification Hubs: the installation ID, which is unique to the app on a given device, and the user ID, which is only added when the user has been previously authenticated. Any other tags that get supplied by the client are ignored, which is by design. (Note that this differs from Mobile Services, where the client could supply any tag and there were hooks into the registration process on the backend to validate tags on incoming registrations.)
Because the client can’t add tags and at the same time there are no service-side hooks into the push notification registration process, the client needs to do the work of adding new tags to a given registration.
So the reason it was not working was because the code in the e-book is out of date and does not work with the latest version.
I had to create an Api Controller to allow the Tag to be registered using the code in the above link. Then the client apps have to call this endpoint just after they have called the two commented methods in the sample code in my question.
I'm really puzzled about to get the whole chain of sending a push from my .NET backend to my android app using Amazon SNS.
I'm using JustSaying on my backend to send my messages to SNS which is working when I tested it with a email subscriper. This is the code I'm using to send the push:
var publisher = CreateMeABus.InRegion(RegionEndpoint.EUCentral1.SystemName)
.WithSnsMessagePublisher<Push>();
publisher.Publish(new Push("Hello"));
And push class looks like this:
public class Push : Message
{
public Push(string body)
{
Body = body;
}
public string Body { get; set; }
}
Now my problem is, how to I send this push to my specific device?
I created an app on https://developers.google.com for google cloud messaging and have a applicationkey and senderid.
I also set up an API project/app on http://console.developers.google.com and enabled the API for cloud messaging.
In AWS SNS console I also created an application and supplied the above mentioned applikationkey which generated a ARN for the Google Android Platform.
In my android app I'm running this code:
try{
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this.getApplicationContext());
String regId = gcm.register("my-sender-id ");
} catch(IOException e){
}
I'm fumbling in the dark here and can't find any good turtorial on how to set this up. AWS documentation seems to cover only small parts and seems to be outdated.
Any help on pointing me in the right direction is greatly appreciated.
Thanks!
See the developer guide of SNS http://docs.aws.amazon.com/sns/latest/dg/mobile-push-gcm.html. In short:
Get GCM device token (or regId) as in your code
Register your device to SNS by calling CreatePlatformEndpointRequest with your device token. You will get a platform endpoint ARN which is the identifier of this device in SNS.
Send notification to the device. Use Publish to publish notification to the platform endpoint ARN
Alternatively, you can subscribe your device to a particular SNS topic. Then you will be able to publish a notification to the topic, and SNS will broadcast it to all subscribers.