如何判断推送通知是否已在 Azure NotificationHubs 中成功传送?

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

我有一个带有简单信使组件的应用程序。当 user1 向 user2 发送消息时,我正在使用 Azure 通知中心向 user2 发送推送通知。由于我正在使用通知中心注册用户的推送通知设备,我不知道他们注册了哪个 phone OS,所以只需为我支持的每种类型排队通知:

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

但是,我们需要处理应用被删除的情况。发生这种情况时,如果无法将推送通知发送到用户的任何设备,我们需要向用户发送电子邮件。

我的问题是:如何判断是否至少有一个通知已成功发送到用户设备?我知道 NotificationHubClient.EnableTestSend 属性,它确实会导致 NotificationOutcome 对象具有成功计数。这会完美地工作,但是 documentation 表明这在生产中不是最佳的:

"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."

如有任何建议,我们将不胜感激!

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

如您所述,NotificationHubClient.EnableTestSend 用于调试并限制为 10 个设备。 如果我们想得到成功发送的次数,我们可以使用函数NotificationHubClient.GetNotificationOutcomeDetailsAsync(string notificationId),更多细节请参考document.

演示代码:

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;

注意:它仅适用于标准定价层。