【问题标题】:Xamarin.iOS push notification tag .NET API / Azure Notification HubXamarin.iOS 推送通知标记 .NET API/Azure 通知中心
【发布时间】:2017-08-29 10:21:50
【问题描述】:

我们正在使用 Xamarin.Forms 开发一个跨平台(Android 和 iOS)应用程序。到目前为止,我们设法让应用程序运行良好,太棒了!

我们使用 Azure 通知中心、GCM(适用于 android)和 APNS(适用于 iOS)在我们的应用中包含了一些推送通知。它几乎可以正常工作!

实际上,我们还有最后一个问题:Android 一切正常,我们也可以使用 iOS 注册推送通知,但我们无法在注册中添加一些标签。

确实,我们需要能够向一个用户或一组用户发送推送通知,而不是发送给所有人。为此,我们在 web api 的方法中这样做:

if (user.DeviceType.Equals("Android"))
{
   registration = new GcmRegistrationDescription(handles.Handle);

}
else
{
   registration = new AppleRegistrationDescription(handles.Handle);
}
registration.Tags = new HashSet<string>();
registration.Tags.Add("usermail:" + user.Email);
registration.Tags.Add("userid:" + user.Id);
registration.Tags.Add("userdevice:" + user.DeviceType);
registration.Tags.Add("usertype:" + tag);
registration.RegistrationId = handles.RegistrationId;
await NotificationHelper.Hub.CreateOrUpdateRegistrationAsync(registration);

对于给定的句柄,我们在 Android 中以这种方式检索它:

protected override void OnRegistered(Context context, string registrationId)
{
   [...] //the registration id is given in args
}

在 iOS 中是这样的:

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

   var DeviceToken = deviceToken.Description;

   if (!string.IsNullOrWhiteSpace(DeviceToken))
   {
      DeviceToken = DeviceToken.Trim('<').Trim('>');
   }

   UserInformations.Handles.RegistrationId = DeviceToken.Replace(" ", "").ToUpper();

   [...]
}

在 Android 中一切正常(我可以添加标签),但在 iOS 中出现错误。线

等待 NotificationHelper.Hub.CreateOrUpdateRegistrationAsync(registration);

正在生成一个异常,告诉我我的 registrationId “不再有效”。您会注意到我在 iOS 的registrationId 中删除了空格,因为如果我不这样做,我会收到另一个错误,告诉我我的registrationID 包含非十六进制字符。

我不知道如何解决这个问题,我是在 iOS 中检索到错误的 registrationId,还是为 APNS 添加不同的标签?

感谢您的帮助!

编辑:我注意到设备令牌必须是大写的。但令人惊讶的是,我得到了同样的错误。这里有2个截图可以帮助你理解:

所以你可以看到,在我的注册中,我在DeviceToken中得到的和我在RegistrationId中得到的是一样的……我不知道该怎么办:/

【问题讨论】:

    标签: asp.net web-applications push-notification xamarin.ios xamarin.forms


    【解决方案1】:

    实际上有很多在线文档和线程帖子告诉您调整从 iOS 方法“RegisteredForRemoteNotifications”获得的设备令牌。但是,如果您查看官方文档,这不是正确的做法。

    下面是我们的“RegisteredForRemoteNotifications”方法的 sn-p,您可以看到我们没有对设备令牌做任何事情,试一试,如果这能解决您的问题,请告诉我。

                if (oldDeviceToken != null)
                {
                    if (oldDeviceToken.ToString() != deviceToken.ToString())
                    {
                        try
                        {
                            Hub.UnregisterAllAsync(oldDeviceToken, (error) =>
                            {
                                //check for errors in unregistration process.
                                if (error != null)
                                {
                                    TestingLogs.ApplicationLog.AppendFile(DateTime.Now.ToString() + "  :  " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + error + " | Source: " + "Unregistering old device token against the notification hub.");
                                    //exit out of the code here because we can't keep our hub clean without being able to remove the device from our registration list.
                                    return;
                                }
                                else
                                {
                                    ShouldComplete = true;
                                }
                            });
                        }
                        catch (Exception genEx)
                        {
                            TestingLogs.ApplicationLog.AppendFile(DateTime.Now.ToString() + "  :  " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + genEx.Message + " | Source: " + genEx + Environment.NewLine + Environment.NewLine);
                        }
                    }
                }
                else
                {
                    // Store current device token 
                    bool res = await ApplicationSettings.StoreDeviceToken(deviceToken);
                }
    
                // Check if we need to perform our initial registrations
    
                if (ShouldComplete)
                {
                    NSSet RegisteredTags = await ApplicationSettings.RetrieveUserTags();
    
                    if (RegisteredTags == null)
                    {
                        RegisteredTags = new NSSet("AppleDevice");
                    }
    
                    //Register the device against the notification hub keeping the details accurate at all times.
                    Hub.RegisterNativeAsync(deviceToken, RegisteredTags, (errorCallback) =>
                    {
                        if (errorCallback != null)
                        {
                            TestingLogs.ApplicationLog.AppendFile(DateTime.Now.ToString() + "  :  " + "[PNS EXCEPTION] - Exception has been hit! - Message: " + errorCallback + " | Source: " + "Registering device token against the notification hub.");
                        }
                        else
                        {
                            if (deviceToken != null)
                            {
                                NSUserDefaults.StandardUserDefaults.SetString("Completed", "InitialTagRegistration");
                                NSUserDefaults.StandardUserDefaults.Synchronize();
                            }
                        }
                    });
                }
    

    【讨论】:

    • 我的decision已经注册了,没问题。我的问题是我需要设备令牌才能在它之后添加标签(如果我执行“发送给所有人”,我的设备已经可以收到推送通知,但现在我需要能够只关注我的设备,这就是为什么我需要令牌)
    • 嗯,我要做的是存储设备令牌,当我准备好更新标签时,我调用一个单独的类,我创建的基本上是针对通知中心重新注册设备。跨度>
    • 但老实说,您需要按原样传递设备令牌,否则会创建不正确的 PNS 句柄。
    猜你喜欢
    • 2018-04-28
    • 2017-06-05
    • 2016-03-12
    • 1970-01-01
    • 2019-08-25
    • 2015-12-11
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多