Xamarin.iOS:远程通知iOS不起作用

时间:2016-06-01 12:07:27

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

我按照xamarin iOS远程通知指南创建了2个证书,一个iOS开发证书和一个APN的开发证书。 在我的钥匙串访问中,我还有2个证书和密钥,我将密钥导出到桌面,因为这是在指南上请求的。 但是当我打开我的应用程序时出现了这个错误

错误:REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION

[Register ("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
   public override bool FinishedLaunching (UIApplication app, NSDictionary options)
   {
      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);
      }

      global::Xamarin.Forms.Forms.Init ();

      LoadApplication (new App ());

      return base.FinishedLaunching (app, options);
}

/// <summary>
///
/// </summary>
public override void ReceivedLocalNotification (UIApplication application, UILocalNotification notification)
{
    // show an alert
    new UIAlertView(notification.AlertAction, notification.AlertBody, null, "OK", null).Show();

    // reset our badge
    UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}

public override void ReceivedRemoteNotification (UIApplication application, NSDictionary userInfo)
{

}

/// <summary>
/// The iOS will call the APNS in the background and issue a device token to the device. when that's
/// accomplished, this method will be called.
///
/// Note: the device token can change, so this needs to register with your server application everytime
/// this method is invoked, or at a minimum, cache the last token and check for a change.
/// </summary>
public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
    // Get current device token
    var DeviceToken = deviceToken.Description;
    if (!string.IsNullOrWhiteSpace(DeviceToken)) {
        DeviceToken = DeviceToken.Trim('<').Trim('>');
    }

    // Get previous device token
    var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");

    // Has the token changed?
    if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
    {
        //TODO: Put your own logic here to notify your server that the device token has changed/been created!
    }

    // Save new device token 
    NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}

/// <summary>
/// Registering for push notifications can fail, for instance, if the device doesn't have network access.
///
/// In this case, this method will be called.
/// </summary>
public override void FailedToRegisterForRemoteNotifications (UIApplication application , NSError error)
{
    new UIAlertView("Error registering push notifications", error.LocalizedDescription, null, "OK", null).Show();
}

}

1 个答案:

答案 0 :(得分:3)

您不应使用iOS模拟器测试远程通知。 它不支持。

请使用实际设备测试推送通知以便工作。

请参阅:click here for more detail