订阅Xamarin Forms中的Azure推送通知服务

时间:2016-07-21 05:22:32

标签: azure push-notification xamarin.ios xamarin.forms azure-notificationhub

我尝试将Push Notifications与我的表单应用程序集成。 Azure消息传递组件用于实现此目的。 以下是我正在使用的代码。我正在获得RegisteredForRemoteNotifications方法的触发器。但是RegisterNativeAsync方法似乎并没有完成这项工作。

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var push = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(push);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else
{
const UIRemoteNotificationType not = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(not);
}
}

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{

        Hub = new SBNotificationHub(conStirng, NotifHubPath);
        Hub.UnregisterAllAsync(deviceToken, (error) =>
        {
            //Get device token
            var id = deviceToken.ToString();
            var tag = "username";
            var tags = new List<string> { tag };
            Hub.RegisterNativeAsync(id, new NSSet(tags.ToArray()), (errorCallback) =>
            {
                if (errorCallback != null)
                {
                    //Log to output
                }
            });
        });
    }

我在这里做错了什么?如何确认注册功能是成功还是失败。?

1 个答案:

答案 0 :(得分:0)

您需要检查来自寄存器方法响应的错误是否为空。如果它为null则表示它是成功的。

var hub = new SBNotificationHub (cs, "your-hub-name");
    hub.RegisterNativeAsync (deviceToken, null, err => {
        if (err != null)
            Console.WriteLine("Error: " + err.Description);
        else
            Console.WriteLine("Success");
    });

对于windows通用应用,我们可以检查响应的registrationId属性。

private async void InitNotificationsAsync()
{
    var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

    var hub = new NotificationHub("<hub name>", "<connection string with listen access>");
    var result = await hub.RegisterNativeAsync(channel.Uri);

    // Displays the registration ID so you know it was successful
    if (result.RegistrationId != null)
    {
        var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);
        dialog.Commands.Add(new UICommand("OK"));
        await dialog.ShowAsync();
    }

}