Azure NotificationHubs how do I get the devicetoken from an iPhone - apple-push-notifications

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.

Related

one signal sdk setup for sending push notification to a particular registered android or ios device

I am very new to xamarin apps, can anyone please tell me how to setup one signal push notification for android and ios. I am able to achieve notification for all the registered device but i want to send notification to a particular registered device. How to achieve this in xamarin android and ios.
If I understood your question right, you want to attach the user data stored on your system by id or email to the device UUID acquired by OneSignal so that you can then target a user from your backend via the OneSignak API.
Xamarin has nothing to do for this, but sending the device UUID to your App backend while the user is logged in, and your App backend should store the UUID with the user data.
void SomeMethod() {
OneSignal.Current.IdsAvailable(IdsAvailable);
}
private void IdsAvailable(string userID, string pushToken) {
//here you can send the userID along with stored login token
//or any kind of id to your backend
}
for more info
https://documentation.onesignal.com/docs/xamarin-sdk

How to sync user's push channels without being able to query Installations

So, quite a straightforward questions, and one that should be simple but Parse doesn't allow querying of the Installation class.
Say I have a single user, and that user has an iPhone and an iPad.
When they register for a push notification channel, the channel is updated on their installation, the same for when they unregister.
My question is, how to I sync these subscriptions across the user's devices when I cannot query the Installation class?
Obviously, I can trigger a sync when the user uses each device, but if they unsubscribe from their iPhone, I don't want them to receive notifications for that channel on their iPad.
Thanks in advance.
You can definitely query the PFInstallation class:
https://parse.com/docs/osx/api/Classes/PFInstallation.html#//api/name/query
I recommend you create a Cloud Code function to be called when a user unsubscribes. To be able to query for installations, you need to use the master key:
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.Installation);

Send push notifications using Registration ID through Azure Notification Hubs

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.

Push notifications to Passbook via Urban Airship not appearing on device

I've found this post to be really helpful in getting set up, but I have yet to see the push notification come through on the iPhone on which the pass is installed.
Passkit-push-notification-not-working-with-urban-airship
I set up my app on urban airship's site pushing to Apple's development servers. I installed a pass on my phone and run the following commands which I found in the above post:
airship = urbanairship.Airship(_UrbanAirshipPassbookKey, _UrbanAirshipPassbookMasterSecret)
airship.push({'aps': {'alert': 'Go.'}}, device_tokens=tokens)
I then see confirmation of this push in the iPhone console window in Xcode.
Received push for topic pass.xxx.xxx: {
...
aps = {
banner = "Hello";
};
and the iPhone then sends its update tag back along with its pass type ID and Device Library ID to the web service. At this point the web service is supposed to send back a list of changed passes. However, I instead see the following error message:
<Warning>: Web service error for pass.mypasstype.id (http://192.168.30.209:8000): Response to 'What changed?' request included 1 serial numbers but the lastUpdated tag (2013-02-11T17:25:25) remained the same.
Does anyone know why this is happening? Do I need to actually modify a field in the pass to get the push notification to appear on the device?
The short answer to your question is yes, you do need to modify a field in the pass to get a push notification to show. This is because, unlike with app pushes, a Passbook push payload does not determine the content of the notification.
The purpose of a Passbook push message is to alert the device that the web service has a new pass with updated content. The alert text is determined solely by the new pass contents. Any content in the push payload is ignored. Apple advise a push notification with an empty JSON dictionary.
Once a push is sent, it triggers the following chain:
Device receives push and queries web service with the passTypeIdentifier and lastUpadted tag
Web service provides a list of serials for all passes with the passTypeIdentifier that have changed since the lastUpdated tag
Device receives serial(s) and requests the web service to send the new .pkpass bundle for each new pass
Web service send the new .pkpass bundle
Device receives the .pkpass bundle and checks it against the old pass for changes
If the following criteria are met, the device will display the notification provided in the changeMessage key:
The value has changed
The changeMessage contains the %# string
Id the %# string is not present, the pass will show a notification Pass Changed. If no changeMessage key is present for the changed value, no message will show.

Handling Toast and Raw Push Notifications

My WP7 app needs to handle different types of push notification. Some of these are only relevant when the app is running but one type serves as a prompt to the user to start the app as well as needing to be handled while the app is running. Hence, when opening the push channel, my code calls BindToShellToast and registers event handlers for when both raw and toast notifications are received:
if (!_pushChannel.IsShellToastBound)
{
_pushChannel.BindToShellToast();
}
_pushChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(pushChannel_HttpNotificationReceived);
_pushChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(pushChannel_ShellToastNotificationReceived);
Looking at the certification requirements, it appears that I have to provide a user setting to allow them to enable/disable not only push notifications in general, but also specifically toast notifications. See Additional requirements for specific app types for Windows Phone (requirement 6.2.1)
Since the user could potentially enable general push notifications but disable toast notifications, it seems to me that my server would need to send both a raw and a toast notification for the type that needs to be handled when the app is not running. This would make registering for the ShellToastNotificationReceived event pointless. Is my thinking correct here?
You should store a settings on your server for each registered device naming what type of notification the user allowed.

Resources