简体   繁体   中英

Notification Hub Delivery failure C#

I have two notification hub clients (nodejs and C#), both used for pushing messages into a hub.

The Node client sends perfectly fine, yet the C# client completes with no message being sent.

Below are the snippets for the used in each client.

C#

NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("<Connection String>", "<Hub Name>");
var notice = @"{'aps':{'alert':'Notification Hub test notification'}}";
var result = await hub.SendAppleNativeNotificationAsync(notice, "<tag>");
Console.WriteLine(result.State);

NodeJS

var azure = require('azure');
var notificationHubService = azure.createNotificationHubService('<Hub Name>','<Connection String>')
var notice = "{'aps':{'alert':'Notification Hub test notification'}}"
notificationHubService.apns.send("<tag>", notice, function(error, res){
    console.log(res);
});

Both work fine when sending Android notifications and messages sent directly from the Azure portal Test feature are fine.

Any assistance would be greatly appreciated.

Duncan,

I think the issue is because your payload is malformed as you are using single quotes. Try the following:

var notice = "{\"aps\":{\"alert\":\"Notification Hub test notification\"}}";

You can try to use the EnableTestSend feature and look at the NotificationOutcome property for the detailed error message. This will send a test send message to 10 devices.

bool enableTestSend = true;
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(connString, hubName, enableTestSend);

var outcome = await hub.SendWindowsNativeNotificationAsync(toast);
Console.WriteLine(outcome.State);

foreach (RegistrationResult result in outcome.Results)
{
    Console.WriteLine(result.ApplicationPlatform + "\n" + result.RegistrationId + "\n" + result.Outcome);
}

TLDR - No '' (single quotes) allowed! Payload must containt alert key

Not a very exciting solution but...

  • If the notification payload contains single quotes even in a string literal the Notification hub will en-queue it without a malformed exception but when it reaches the APNS it will not be delivered as the payload is invalid according to them.
  • If the notification payload doesn't have alert key present it will also be en-queued but not delivered by the APNS

As already mentioned in my colleagues answers, you need to use double quotes in C# as a first step.

Second, you also need to escape the json characters: Your payload should look like this:

var notice = "{{\"aps\":{{\"alert\":\"Notification Hub test notification\"}}}}";

Basically you escape the json braces { } by adding additional ones {{ }}.

This will send a valid json payload to the SendAppleNativeNotificationAsync() method. The payload now sent looks like this:

{"aps":{"alert":"Notification Hub test notification"}} (which is the correct notification format on iOS)

Here are the valid json payloads for iOS from the Apple Developer webpage.

You could always test if the json you are sending is a valid notification payload by using the 'Test Send' functionality in the Azure Notification Hub you are using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM