【问题标题】:Subscribing to Azure Push Notification service in Xamarin Forms在 Xamarin Forms 中订阅 Azure 推送通知服务
【发布时间】:2016-11-24 12:40:44
【问题描述】:

我尝试将推送通知集成到我的表单应用程序中。 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
                }
            });
        });
    }

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

【问题讨论】:

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


    【解决方案1】:

    您需要检查注册方法的响应错误是否为空。如果为 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();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 2019-10-23
      • 2017-05-23
      • 2017-02-22
      • 2016-06-05
      • 2015-06-19
      相关资源
      最近更新 更多