Send push notifications using Registration ID through Azure Notification Hubs - windows

I am trying to use Azure Notification Hubs to send push notifications to a client. I read this article which uses tags to identify each user.
https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-aspnet-backend-windows-dotnet-notify-users/
It does the work, but the number of tags is limited. I was thinking to store and use the Registration ID that the Hub returns.
Is there any way to send notifications using this ID?
Another way would be using the Channel.URI that is returned by WNS. Can this be implemented somehow?

Actually NH limits only number of tags per single registration but per hub you may have as many registrations as you need and each registration may have unique tag which you can use to route the notifications.
Also there is new Installation API for Notification Hubs which I believe fits better for you. It is still not well-documented but well-done and ready to use. Here you can find short description of how to use that API. Readme is about Java but .NET SDK has pretty much the same capabilities (in the end both call same REST API).

Keyword is TAG ! If you use any spesific tag for any registered device which is Android,IOS,Windows OS etc, you can send notification to any specific device.
To do these, you should follow below steps one by one ;
As Client side, register device using a spesific tag to selected Azure Notification Hub
Client Example for Android :
`/*you don't have to use Firebase infrastructure.
You may use other ways. It doesn't matter.*/`
String FCM_token = FirebaseInstanceId.getInstance().getToken();
NotificationHub hub = new NotificationHub(NotificationSettings.HubName,
NotificationSettings.HubListenConnectionString, context);
String registrationID = hub.register(FCM_token, "UniqueTagForThisDevice").getRegistrationId();
Like you see, we have used a unique tag call "UniqueTagForThisDevice" for selected Android device.
As Server Side, you should send notification using that TAG call "UniqueTagForThisDevice".
Server Example using Web API to send push selected Android device :
[HttpGet]
[Route("api/sendnotification/{deviceTag}")]
public async Task<IHttpActionResult> sendNotification(string deviceTag)
{
//deviceTag must be "UniqueTagForThisDevice" !!!
NotificationHubClient Hub = NotificationHubClient.CreateClientFromConnectionString("<DefaultFullSharedAccessSignature>");
var notif = "{ \"data\" : {\"message\":\"Hello Push\"}}";
NotificationOutcome outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif,deviceTag);
if (outcome != null)
{
if (!((outcome.State == NotificationOutcomeState.Abandoned) ||
(outcome.State == NotificationOutcomeState.Unknown)))
{
return Ok("Push sent successfully.");
}
}
//Push sending is failed.
return InternalServerError();
}
As last, you should call above Web API Service method using "UniqueTagForThisDevice" tag from any helper platform (Postman, Fiddler or anothers.).
Note : TAG doesn't have to be deviceToken or similar things. It just have to spesific for each devices. But I suggest you that, if you use WebAPI and it is related with Owin midlleware, you may prefer username as unique tag. I think, this is more available for application scenarios. In this way, you can carry sending notifications from unique devices to unique users ;)
That's all.

Related

Push Notification in Xamarin using AWS SNS

I provide various rental space, and my app provides the feature to rent this space to people. Let's say a user with higher priority/ memebrship in my app trying to book a space that is being used up by lower priority user. As soon as the higher priority user press the book button I want a notification to pop up in the lower priority users mobile.
My app is build using Xamarin Forms. And I want to push notification using AWS SNS, but as I see SNS requires device token to send the notification. I am planning to store the device token in the database for ever user, but I am not entirely sure how to get device token depending upon both IOS and Android environment. I am thinking of using a dependency interface that
public interface INotificationService
{
Task<string> GetDeviceToken();
}
And I have not find a good source which I can use to get device token.
Can anyone help me, and correct me if it is correct to save device token in database?
I am working on this right at this moment. This is for iOS only and what's working for me.
Put this into your AppDelegate.
public async override void RegisteredForRemoteNotifications(UIApplication application, NSData token)
{
if (application.IsRegisteredForRemoteNotifications == true)
{
var snsClient = new AmazonSimpleNotificationServiceClient("your aws key id", "your aws secret key", Amazon.RegionEndpoint.YourRegion);
/* In the AWS SNS example here - https://docs.aws.amazon.com/mobile/sdkforxamarin/developerguide/getting-started-sns-ios.html
token.Description is used, for me the line below triggers an exception, so I used token.DebugDescription instead.
*/
var deviceToken = token.DebugDescription.Replace("<", "").Replace(">", "").Replace(" ", "");
if (!string.IsNullOrEmpty(deviceToken))
{
//register with SNS to create an endpoint ARN
var response = await snsClient.CreatePlatformEndpointAsync(
new CreatePlatformEndpointRequest
{
Token = deviceToken,
PlatformApplicationArn = "your aws platform application arn"
});
}
}
}
As for storing it in a database, I haven't got that far, but I am think of using DynamoDb for my specific purpose.
When/if I have Android working, I will update my answer.

Azure NotificationHubs how do I get the devicetoken from an iPhone

I am using Azure NotificationsHubs for iOS push notifications and want to get the deviceToken to register the device, along with the user, in a table through an api so I can send notifications to specific users/devices and keep track of badge counts. When not using Azure Notification Hubs.
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
print("DeviceToken: \(deviceToken)")
}
I see the documentation says:
By default, the SDK will swizzle methods to automatically intercept
calls to UIApplicationDelegate/NSApplicationDelegate for calls to
registering and intercepting push notifications, as well as
UNUserNotificationCenterDelegate methods. Note this is only available
for iOS, watchOS, and Mac Catalyst. This is not supported on macOS and
tvOS.
I'm not sure what swizzling means but I don't want to disable what I have working thus far. Is there another way within the standard implementation to get the deviceToken?
I also see this in the documentation:
To target a particular user on the backend, you can specify a tag such as $UserId:{VALUE} where VALUE is the user name you have specified, just as you can target an installation using the $InstallationId:{VALUE} tag.
But how do I get the InstallationId and is that different from the deviceId or value I use in xcrun simctl push? I expect somewhere I will need to store it on the server side and associate it with a user or something.
I read this post which states:
When you send a notification from the server, one of the paramters is the device ID.
I could do it by user only, but what if they want different notification preference for different devices?
I expect to send to a specific user on a specific device from the server you would use tags, for example:
Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
String userTag = "(UserId:xxxx)";
// substituting for iOS
var toast = "{\"aps\":{\"alert\":\"This is a test\"}}";
outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, userTag);
On the client, I am setting the user id like this:
let userId = "xxxx"
MSNotificationHub.setUserId(userId);
Even without the device id part of it, I can't get the user part working. I can send a notification without any tags, but I add in the user tag and it does not work. I assumed by calling setUserId that would add a tag, based on the links above.

Notification Hub Help on Xamarin

I have Azure notification hub working on Xamarin Forms for iOS and Droid to receive general push notifications. I am trying to send a POSTID in my payload and then take that ID and direct to the data behind it. My issue, I cannot seem to get the postid to read into the app. Every time I get an empty ID. The Push notification an Droid and are received, but the extra data, like postid are not.
{"aps":{"alert":"Test #03_01","postid":"8921"}}
Can someone point me to some documentation on this? How to make it work with ID/data in the push notification behind the scene.
You'll need to promote your custom data fields. At the moment, they are in the "aps" section, which is reserved for data that will be consumed by APNS or iOS. Somewhere along the line your fields are being stripped.
Here's the APNS documentation for building request payloads.
For you it should look something like:
{
"aps": {
"alert":"Test #03_01"
},
"postid":"8921"
}

Azure Notification Hub test send with tags

Both Visual Studio and Azure Management Portal have functionality to send test push notifications.
When I do broadcast, everything works fine. But when I try to send it by tag, nothing happens.
I tried to send message with specific tag via .NET object and it also works just fine, both tags list and tag expressions work as expected.
string tagsExpr = "mytag";
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
hub.SendTemplateNotificationAsync(templateParams, tagsExpr);
Is there some specific format of tags in that fields? I cannot find any information about that.
In your first screenshot, we could find you send a test notification to Android platform in Azure portal, you said no devices receive the notification. Please check all registrations and view the tags they are registered for to make sure the GCM native registrations with mytag are in Registrations list.

how to send a push notification message to the specific target via APN ?

We're planning to develop a new application.
this application will use a lots of different companies but application will be unique.let me explain it : our users will download this application from app store
after login process we will be able to understand which companies are using this application and who is on the line at the moment..
so my question is if I want to send a push notification to a specific company and it's users who use our application, how can we do that ?
let me give you an example :
We assume that there are 3 companies
First is A , second is B and other one is C
A has 10 users , B has 4 users and C has 40 users.
I want to send a push notification TO C's users ( in this example..).. A , B and C are using same application but their credentials are different from each other.
is it possible with your PN infrastructure ? Can you share with me your opinions ? My best
This is a very very broad question,
but to lead you in the right direction, this is possible. It all depends on your method of implementing Apple's APN service. The push notification service is simply an API you can submit HTTPS requests to and it pushes the notification out to whatever devices you supply it.
The logic for whom receives these notifications is all up to you in your server side programming.
For example:
I am developing an app right now that uses a NodeJS server with the node-apn module installed. With this, I can connect to my Database where I store users device tokens (used to specify which device you are pushing to) then send a simple notification like so:
var device = new apn.Device("DEVICETOKEN");
var note = new apn.Notification();
note.payload = {custom:"objects", cango:"here", numval:56};
note.expiry = Math.floor(Date.now()/1000)+10; // 3 hours from now
note.badge = 3;
note.sound = "ringtone.wav";
note.alert = "This is a push notification";
apnConnection.pushNotification(note, device);
You should really do a little research before coming to SO with nothing, then expecting answers like this all the time when you haven't put any effort into it.
You will have to upload each app to the apple store, meaning that each company will have to open development account in apple.
Companies will send you a requested licence for the app and for the server.
You will have to compile our app with this licence.
of course every app will have a apple ID.
You can send message from the server to gateway.sandbox.push.apple.com, port 2195.

Resources