ios - 来自 Azure 通知中心的 Xamarin.iOS 推送通知

标签 ios azure push-notification xamarin.ios apple-push-notifications

我正在尝试使用本教程向我的 Xamarin.iOS 应用程序添加推送通知 https://learn.microsoft.com/en-us/azure/notification-hubs/xamarin-notification-hubs-ios-push-notification-apns-get-started

我已按照原样执行了所有步骤,以下是我迄今为止尝试过的:-

AppDelegate.cs

public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
  {
   // create a new window instance based on the screen size
   Window = new UIWindow(UIScreen.MainScreen.Bounds);
   if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
   {
    var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
        UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
        new NSSet());

    UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
    UIApplication.SharedApplication.RegisterForRemoteNotifications();
   }
   else {
    UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
    UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
   }


   // If you have defined a root view controller, set it here:
   UIStoryboard storyboard = UIStoryboard.FromName("Main", null);
   var myViewController = storyboard.InstantiateViewController("Login_VC") as LoginViewController;
   Window.RootViewController = myViewController;

   // make the window visible
   Window.MakeKeyAndVisible();



   return true;
  }

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
  {
   Hub = new SBNotificationHub(ServiceConstants.ListenConnectionString, ServiceConstants.NotificationHubName);

   string token = deviceToken.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");

   deviceToken = NSData.FromString(token);
   Hub.UnregisterAllAsync(deviceToken, (error) =>
   {
    if (error != null)
    {
     Console.WriteLine("Error calling Unregister: {0}", error.ToString());
     return;
    }

    NSSet tags = null; // create tags if you want
    Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>
    {
     if (errorCallback != null)
      Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
    });
   });




  }

我已使用 Apple 开发者控制台中的开发配置文件对我的应用程序进行了签名,该配置文件是使用启用了推送通知的 AppId 进行签名的。

但是当我在注册推送通知时一开始就遇到错误,如下所示:-

  Error Response:<Error><Code>401</Code><Detail>ExpiredToken: .TrackingId:94e8c47b-1221-4188-8a75-9b744e12c9d7_G4,TimeStamp:1/20/2017 10:05:51 AM</Detail></Error>
Fail to perform registration operation.
<NSMutableURLRequest: 0x170018a70> { URL: https://mynotificationnamespace.servicebus.windows.net/mynotificationhub/Registrations/?$filter=deviceToken+eq+'62383062663335386437646233313263613766656165323535626634646537393438623262616238393164393466393766353030666166313966623339323232'&api-version=2013-04 }
 Headers:{
    Authorization = "SharedAccessSignature sr=http%3a%2f%2fmynotificationnamespace.servicebus.windows.net%2fmynotificationhub%2fregistrations%2f%3f%24filter%3ddevicetoken%2beq%2b%2762383062663335386437646233313263613766656165323535626634646537393438623262616238393164393466393766353030666166313966623339323232%27%26api-version%3d2013-04&sig=dpGnfYj19D%2FseR3GlTrE6Ay0ASdc%2BwFwWRNf3sxBE0%3D&se=148394919&skn=DefaultListenSharedAccessSignature";
    "Content-Type" = "application/xml";
    "User-Agent" = "NOTIFICATIONHUBS/2013-04(api-origin=IosSdk; os=iOS; os_version=10.2;)";
}

我能够从 Xamarin.Android 的同一个通知中心获取推送通知,但不能从 Xamarin.iOS 获取推送通知。

如有任何帮助,我们将不胜感激。

最佳答案

回答我自己的问题,因为它可能对寻找类似内容的人有用。事实证明,我的设备上禁用了自动日期时间,因此设备 token 获取为无效或已过期,因为设备上的时间滞后了 30 分钟。

关于ios - 来自 Azure 通知中心的 Xamarin.iOS 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41761287/

相关文章:

iPhone - 在远程推送通知对话框中查看/关闭按钮本地化

ios - UISearchBar 在点击时消失

ios - 当用户输入格式为 (1,000) 的数字(如 (1000))时,将逗号添加到计算器中的字符串

ios - UIPropertyAnimator 导致崩溃

azure - 如何将 Xamarin 应用连接到 SQL Azure 数据库

azure - 节点没有足够的临时存储空间用于临时存储

javascript - 当语法有效时,Azure 函数提示 JS 语法错误

php - 苹果推送通知问题

ios - Firebase FCM 最初无法工作,但如果打开应用程序,则可在 3-4 次后工作

php - 推送通知在android中多次获取