Azure push notifications + .NET MVC - how to debug or resolve - asp.net-mvc

We are attempting to send push notifications via Azure Notification Hub and aren't able to send messages from Azure.
We've tried debugging but have been unable to figure out the error from the documentation.
How can we debug this further? We've walked through MSDN's documentation in detail but haven't been able to come up with anything
Here's our process:
We have the certificate setup in our iphone app and in Azure Notification Hub
We create our hub connection via NotificationHubClient with the debug option enabled before any Azure calls
NotificationHubClient.CreateClientFromConnectionString(NOTIFICATION_SERVICE_CONNECTION_STRING, NOTIFICATION_SERVICE_NAME, true)
We register the device upon logging into the app (and we're updating the push id if it changes)
//simplified process showing a new registration only
RegistrationDescription registration =
Hub.CreateAppleNativeRegistrationAsync(data.PushId, new List<string>() { "member:10" });
registration.RegistrationId = Hub.CreateRegistrationIdAsync().Result;
var result = Hub.CreateOrUpdateRegistrationAsync(registration).Result;
Result: We appear to get back a successful RegistrationDescription that shows our registration id, tags, device token (pushId), etag and registration id. We do show successful registration operations in the Azure Monitor.
Note: After registering the device, we don't see an increase in active devices or registrations in our Azure dashboard (0 Active Devices and 0 registrations in the dashboard)
We send a notification
// example apns: {"aps":{"alert":{"title":"TESTING","body":"Test notification"}},"MemberId":150888,"RelatedId":1,"NotificationType":2,"NotificationTypeId":2}
var notification = Hub.SendAppleNativeNotificationAsync(apns.ToJson(), "member:10").Result;
Result: We normally receive a result similar to below, however a notification is not sent
"Notification state: DetailedStateAvailable, success: 1, tracking id: xxxxxx-xxxx-xxxx-xxxx-xxxxxx, notification id: {empty} Platform: apple, Device reg id: {removed for security}, Result: The Notification was successfully sent to the Push Notification System, PnsHandle: xxx"
Other Notes: We do see several "Invalid Channel Errors", "APNS Bad Channel Error" and "APNS Expired Channel Errors" but cannot find the source. We also see several Successful Notifications logged in Azure, but never receive the notification.

The success states that you were able to request a send.
The Invalid Channel Errors and APNS Bad Channel Error indicate that you have an invalid device token. This typically happens because:
You have told NH you are using the APNS sandbox, but your app is getting tokens for APNS production.
OR
Reverse the states in #1 (NH thinks you have APNS production, your app is getting tokens for APNS sandbox)

Related

Error 52134 Invalid APNs device token in Flutter "twilio_voice"

I'm using "twilio_voice" for a flutter application for generating and receiving call from app to real number and vice versa.
When I'm trying to call to a real number from app it's working fine. But when i'm trying to receive call within the app, it's not working.
When I checked at Twilio console I found "Error 52134 Invalid APNs device token".
I have already created the VOIP certificate in Appstore console and also generated a Push Credential in Twilio console. The SID also used in the backend.
In my applog I also found "Successfully registered for VoIP push notifications." as a confirmation.
But still not working.
After your app has register for the push notification, you will need to create a binding with the Push Token to the Twilio server. For that you will need an intermediate server of your own(Can be a Node js/PHP/Python server application). First you will make a rest api request to your own server from the app. The server application will send a binding request to Twilio server. Once that is done, you will receive push notification every time there is an incoming call via didReceiveRemoteNotification() delegate in AppDelegate.
For the server application sample and Rest api sample for iOS app, follow this documentation from Twilio:
https://www.twilio.com/docs/notify/register-for-notifications-ios
Got the solution.
The issue was in the registering device. I was sending fcm token to the server for registering device. But after changing it device UUID, it worked.
Also point to be noted that, you will need a different SID from Twilio for development and Testflight. SID from Sandbox credentials won't work if you running app from TestFlight.

APNS Bad Channel Error on Notification Hub Test Push Send

I am developing a cross-platform application with xamarin forms and I'm trying to run notifications on the various devices through the notification hub.
I encounter a problem when I try to send a test notification for the iphone.
The device can register the token, received by apns, on the notification hub even if only after the '<' and '>' and spaces are deleted, otherwise, the notification hub returns the error "one or more characters in device token is not a hexadecimal digit".
However, when I try to send a first test notification, Notification Hub returns:
"The test message has been sent. Outcome: {1 success, 0 unsuccessful}"
but notification never reaches the device. All the test notifications sent subsequently result:
"The test message has been sent. Outcome: {0 success, 0 unsuccessful}"
In fact, going to see the metrics in the notification hub the next day there are some
"APNS bad Channel Error"
So what could be the problem? Did anyone have the same problem? And what does "APNS bad channel error" mean?
The APNS Bad channel error indicates the device token you are using is no longer valid. Your app on the device needs to update the token on app. You can include that as part of application restart.
Also, regarding the notication not reaching the device. Can you make sure the device is switched on and notification are turned on. You can try to set the apns-expiration header to a longer value so that apns stores the notification for redelivery for a longer time.
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW13
Thanks,
Amol

App Deletion - APNS Response to Development Server on Push Notification Failure

I have an iOS app that receives notification from APNS. If my user deletes the app there is no way for the backend to know the app is not installed on a certain device(device token). In this case what error code will be generated when trying to push via APNS, so that i can remove the entry from the database?
There is a Provider (Your backend server) API introduced by Apple in WWDC-2015 & enhanced in 2016 to give more valuable feedback to the server about the push notification. Here is a transcript to that WWDC session.
From the transcript: "If a device token has been removed, you will get an HTTP/2 response with status 410, or "removed."
It will have a time stamp in payload indicating when APNS last learned that the device token has been removed."
APNS Server Response Codes
200 Success
400 Bad request
403 There was an error with the certificate or with the provider
authentication token.
405 The request used a bad :method value. Only POST requests are
supported.
410 The device token is no longer active for the topic.
413 The notification payload was too large.
429 The server received too many requests for the same device token.
500 Internal server error
503 The server is shutting down and unavailable.
Now what I cannot confirm to you is that if iOS removes the device token if app is removed or if notification setting is turned off from App settings without deleting app.
"410 does mean the app was uninstalled. The token will remain active if the user disables notification alerts in the app settings. The device will still receive the notification, even if no alert is shown to the user. The server will not know if the user has turned off notification alerts. Only the app knows this."
Thanks to Marcus Adams for clartfying this doubt.
Here goes the Apple Developer Guide!!!
If required, Here is a Paid SDK that can help you with uninstallation tracking.

Diagnosing dropped notifications from Azure Notification Hub to APNS

We have a (mostly) successful implementation of push notifications to iOS and Android devices through Azure Notification Hubs.
The problem is that some of the iOS devices are apparently never receiving notifications that are sent by Azure Notification Hubs.
We use templates and tags to direct the messages to the appropriate devices. The tags are interest topics, and never user-specific, so we're expecting one notification for a tag to be pushed to all devices subscribed to that tag.
The Android devices seem to receive their notifications flawlessly, but the iOS devices are not consistent. Most of them work. A couple do not.
We are well aware that push notifications are delivered with best effort and have no guarantee of reliability, but our limited testing has revealed more devices which consistently fail to receive push notifications than seems unreasonable (more than two failures from about a dozen devices).
Here's the setup:
We have a simple C# routine in the back end which connects to Azure Notification Hubs and sends notifications to Azure:
var outcome = await hub.SendTemplateNotificationAsync(properties, tag);
We have used the GetAllRegistrationsAsync method to make sure that every device we are checking has successfully registered and is using the correct template. Every device is registered, all the templates are correct.
We are not in "test mode"; the enableTestSend parameter of NotificationHubClient.CreateClientFromConnectionString is set to False.
Troubleshooting:
When we send the notification out, most devices receive the notification and, in the specific case we're testing, update the badge counter with the correct number.
However, a couple of devices do not seem to get the notification. One of the devices did get the notification after we rebooted the device, but after that it stopped.
Using the above mentioned GetAllRegistrationsAsync method, we have verified that the problem devices are correctly registered on Azure and have the correct tags and templates.
We were able to determine the device tokens of the problem devices from the Azure registrations. We used a PHP script which communicates directly with APNS to send a notification just to the problem devices using their device tokens. Every time, the device receives this direct-send notification. It's only the notifications from Azure which are unreliable.
When we examine the Azure Notification Hub Monitor page, we see these metrics for the past 24 hours:
967 APNS Successful Notifications
3 APNS Bad Channel Errors
2 APNS Expired Channel Errors
4 APNS Errors
... and no other errors reported for APNS or for Azure in general. The failure rate we're seeing should have produced an error count over 20.
We have not been able to determine which device tokens were responsible for the errors; is there a way to get this information from Azure?
We're at a loss to explain why we can send notifications directly to these devices over APNS itself, but not through Azure, and why it is that Azure doesn't report more errors than it does.
Any suggestions or insights?
It's quite possible that you have some sandbox device tokens in your database (I'm not sure if the device tokens are stored in your server or in Azure Notification Hub). When trying to send a notification with a sandbox device token to the production push environment, an InvalidToken error is returned by Apple, and the connection is closed.
Very often, by the time the server that sends push notifications to Apple's APN server gets the error response, it has already sent many more notifications (possibly with valid tokens), and all of which are discarded by Apple. At this point, new notifications are accepted by Apple only after a new connection with APNS is established, so messages that were sent after the invalid token to the old connection need to be resent. It is possible Azure don't handle this resending correctly.
As you said, the Azure Notification Hub Monitor page shows a few errors. I suspect that 3 APNS Bad Channel Errors means invalid device tokens. I don't know how many invalid device tokens you actually have in the DB, but even one can cause many notifications with valid tokens not to be accepted by Apple.
The best solution is to test all the device tokens in the DB and figure out the ones that are invalid and delete them.

Can provider send phone number along with device token to APNs server for push notification ?

iOS Device Token for the push notification is an opaque identifier of a device.
The provider can't get any device related information from the device token.
Based on Apple Push Notification programming guide, the provider needs to provide device token and payload to APNs server for the push notification.
Can provider send phone number OR bundle id OR advertising identifier OR vendor identifier to the APNs Server when the provider send token and payload to APNs server during push notification ?
So, the APNs server will not send the message to device if the device token is not associated with any device properties (like phone number or advertising identifier or vendor id).
I think you answered your own question:
Based on Apple Push Notification programming guide, the provider needs to provide device token and payload to APNs server for the push notification.
If you could provide the phone number, for example, you could simply use harvested phone numbers and spam people with push notifications. The token is not only for identifying the phone; you only get one if the user explicitly said "Yes, I want push notifications."
The only properties that determine if a push notification is delivered to an iOS application on a given device are the push certificate used to establish the connection with APNS (which determines the destination application) and the device token (which determines the destination device). Any other parameters you choose to include in your payload will have no effect on the delivery of the message.
The solution that we implemented (and it works very well), is:
have the device token sent to the server upon registration
server stores that in a 'user_device' table (which ties the user to the device - once you get the info.... at this time you only have the token, so the userId is 0)
the user continues with their login info.....
as the user completes their login (going to the server for verification), include the token and match that up in the database (note that you could also just send it at this point, but we like to send the token in #2 so we could see how many people register devices but not log in...)
Now, you have a match-up from the user data to their deviceId and can then match up various USER properties (or even the device properties if you have gotten them from other means - like when the user signed in and completed their profile) and send the appropriate push notice.

Resources