lehn0058
lehn0058

Reputation: 20237

How can I tell if a push notification was successfully delivered in Azure NotificationHubs?

I have an app with a simple messenger component to it. When user1 sends a message to user2 I am using Azure Notification Hubs to send a push notification to user2. Since I am using Notification Hubs to register the user's devices for push notifications, I don't know which phone OS's they have registered with, so just queue a notification for each type I support:

NotificationOutcome outcome1 = await hub.SendAppleNativeNotificationAsync(jsoniOSPayload, tags);
NotificationOutcome outcome2 = await hub.SendGcmNativeNotificationAsync(jsonAndroidPayload, tags);
...(etc.)

However, we need to handle the case where an app has been deleted. When this happens, we need to send the user an email if a push notification could not be sent to any of their devices.

My question is: how can I tell if at least one notification was successfully delivered to a users device? I know about the NotificationHubClient.EnableTestSend property, which does cause the NotificationOutcome object to have a success count. This would work perfectly, but the documentation indicates this would not be optimal in production:

"When test send is enabled, the following occurs: All notifications only reach up to 10 devices for each send call.The Send* methods return a list of the outcomes for all those notification deliveries. The possible outcomes are the same as displayed in telemetry. Outcomes includes things like authentication errors, throttling errors, successful deliveries, and so on.This mode is for test purposes only, not for production, and is throttled."

Any suggestions would be appreciated!

Upvotes: 1

Views: 2167

Answers (1)

Tom Sun
Tom Sun

Reputation: 24529

how can I tell if at least one notification was successfully delivered to a users device?

As you mentioned that NotificationHubClient.EnableTestSend is used for debugging and limited to 10 devices. If we want to get the count of successfully delivered, we can use function NotificationHubClient.GetNotificationOutcomeDetailsAsync(string notificationId), more details please refer to document.

Demo code:

NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("Endpoint=sb://notificationnamespace.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=xxxxxxx", "NotificationHub Name");
string message = "{\"title\":\"((Notification title))\",\"description\":\"Hello from Azure\"}";
           var result = await hub.SendGcmNativeNotificationAsync(message); //GCM for example.
           var notificationDetails = await hub.GetNotificationOutcomeDetailsAsync(result.NotificationId);
           return notificationDetails;

enter image description here

Note: It is just for standard pricing tier.

Upvotes: 4

Related Questions