Asp.net Xamarin.iOS推送通知标签.NET API/Azure通知中心

Asp.net Xamarin.iOS推送通知标签.NET API/Azure通知中心,asp.net,web-applications,push-notification,xamarin.ios,xamarin.forms,Asp.net,Web Applications,Push Notification,Xamarin.ios,Xamarin.forms,我们正在使用Xamarin.Forms开发一个跨平台(Android和iOS)应用程序。到目前为止,我们设法让应用程序运行良好,所以它很酷 我们在应用程序中包括了一些推送通知,使用Azure通知中心、GCM(适用于android)和APNS(适用于iOS)。它几乎可以正常工作 实际上,我们还有最后一个问题:安卓系统一切正常,我们也可以使用iOS注册推送通知,但我们不能在注册中添加一些标签 事实上,我们需要能够将推送通知发送给一个用户或一组用户,而不是发送给每个人。为此,我们使用web api的一

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

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

实际上,我们还有最后一个问题:安卓系统一切正常,我们也可以使用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);
这样在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();

   [...]
}
public override void registeredForremotentifications(UIApplication应用程序,NSData deviceToken)
{
[...]
变量DeviceToken=DeviceToken.Description;
如果(!string.IsNullOrWhiteSpace(DeviceToken))
{
DeviceToken=DeviceToken.微调(“”);
}
UserInformations.Handles.RegistrationId=DeviceToken.Replace(“,”).ToUpper();
[...]
}
Android中的一切都很好(我可以添加标签),但我在iOS中遇到了一个错误。线路

等待 NotificationHelper.Hub.CreateOrUpdateRegistrationAsync(注册)

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

我不知道如何解决这个问题,我是在iOS中检索错误的注册ID,还是为APN添加标签的方式不同

谢谢你的帮助

编辑:我注意到设备令牌必须是大写的。但令人惊讶的是,我也犯了同样的错误。以下是帮助您理解的两个屏幕截图:


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

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

下面是我们的“RegisteredForRemoteNotifications”方法的一个片段,您可以看到我们对设备令牌不做任何操作,请尝试一下,如果这解决了您的问题,请告诉我

            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();
                        }
                    }
                });
            }

我的决定已经注册了,没有问题。我的问题是,我需要设备令牌,以便能够在它之后添加标记(如果我执行“发送到所有人”,我的设备已经可以接收推送通知,但现在我需要能够只关注我的设备,这就是我需要令牌的原因)我要做的是存储设备令牌,当我准备好更新标记时,我会调用我创建的一个单独的类,基本上是针对通知中心重新注册设备。但老实说,您需要按原样传递设备令牌,否则它可能会创建不正确的PNS句柄。
            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();
                        }
                    }
                });
            }