【问题标题】:Push notifications on Xamarin IOS在 Xamarin IOS 上推送通知
【发布时间】:2014-11-24 09:12:04
【问题描述】:

我们正在使用 Xamarin.Forms 在 Xamarin 上开发一个跨平台(ios 和 android)应用程序。我们已经设法让相同的应用程序在 IOS 和 Android 上运行。太好了!

我们希望在我们的应用中包含推送通知,这已经在 Android 中运行。然而,对于 IOS,这是一个完全不同的故事。在任何地方都找不到简单的库!

对于 android,我们使用 Redth 的 Google Cloud Messaging Client,这个库非常易于使用。我们让它在 2 小时或更短的时间内运行。但是,对于 IOS,找不到类似的东西。

如何在 xamarin 中为我的 IOS 设备注册推送通知?我们已经拥有正确的证书等。它只是我们需要开始工作的设备端。有什么可以为我指明正确的方向吗?

【问题讨论】:

  • 如果您按照正确的步骤操作但仍然无法正常工作,请检查 APNS 日志是否有错误。
  • 问题是我根本不知道如何接收任何推送通知。在 android 上,您只需注册一个服务(后台进程)并处理所有推送通知。在IOS上,仍然没有线索..

标签: c# android ios parse-platform xamarin


【解决方案1】:

使用此push notification 链接在 IOS 中获取或启用推送通知服务。

您需要执行以下步骤:

1> 创建和下载 SSL 和 APNS 证书以及启用推送通知的配置文件。

2>首先双击SSL证书而不是APNS证书而不是配置文件。

3.> 现在从钥匙串访问导出 p12 文件并从命令提示符创建 PEM 文件。

4>现在注册FinishedLaunching内的推送通知。

5.> 运行你的程序,你会得到一个设备令牌并发送给服务器。

现在,APNS 服务器将分别发送此通知到令牌。

【讨论】:

    【解决方案2】:

    您可能需要查看 PushSharp 以支持 iOS 和 Android 通知的服务器端部分。我相信他们还有一个 Xamarin.iOS 库,您可以使用它来订阅推送通知。

    【讨论】:

      【解决方案3】:
          // Register for push notifications.
              public override bool FinishedLaunching(UIApplication app, NSDictionary options)
          {
              if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
              {
                  var authOptions = UserNotifications.UNAuthorizationOptions.Alert | UserNotifications.UNAuthorizationOptions.Badge | UserNotifications.UNAuthorizationOptions.Sound;
                  UserNotifications.UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
                  {
                      Console.WriteLine(granted);
                  });
                  UIApplication.SharedApplication.RegisterForRemoteNotifications();
              }
              else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
              {
                  var settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());
                  UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
                  UIApplication.SharedApplication.RegisterForRemoteNotifications();
              }
              else
              {
                  var notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                  UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
              }
      
      }
      private SBNotificationHub Hub { get; set; }
              public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
              {
      
                  Hub = new SBNotificationHub(Constants.ListenConnectionString, Constants.NotificationHubName);
      //if user is not logged In
                  Employee employee = JsonConvert.DeserializeObject<Employee>(Settings.CurrentUser);
                  if (employee != null)
                  {
      
                      NSSet tags = new NSSet(new string[] { "username:" + employee.Email }); // create tags if you want
                      Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) =>
                          {
                              if (errorCallback != null)
                                  Console.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
                          });
                  }
                  else
                  {
                      Hub.UnregisterAllAsync(deviceToken, (error) =>
                      {
                          if (error != null)
                          {
                              Console.WriteLine("Error calling Unregister: {0}", error.ToString());
                              return;
                          }
                      });
                  }
              }
      
      
      public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
              {
                  AzurePushNotificationManager.RemoteNotificationRegistrationFailed(error);
              }
              public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
              {
                  ProcessNotification(userInfo, false);
              }
              void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
              {
                  // Check to see if the dictionary has the aps key.  This is the notification payload you would have sent
                  if (null != options && options.ContainsKey(new NSString("aps")))
                  {
                      //Get the aps dictionary
                      NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
      
                      string alert = string.Empty;
      
                      if (aps.ContainsKey(new NSString("alert")))
                          alert = (aps[new NSString("alert")] as NSString).ToString();
      
                      if (!fromFinishedLaunching)
                      {
                          //Manually show an alert
                          if (!string.IsNullOrEmpty(alert))
                          {
                              NSString alertKey = new NSString("alert");
                              UILocalNotification notification = new UILocalNotification();
                              notification.FireDate = NSDate.Now;
                              notification.AlertBody = aps.ObjectForKey(alertKey) as NSString;
                              notification.TimeZone = NSTimeZone.DefaultTimeZone;
                              notification.SoundName = UILocalNotification.DefaultSoundName;
                              UIApplication.SharedApplication.ScheduleLocalNotification(notification);
                          }
                      }
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-26
        • 1970-01-01
        • 1970-01-01
        • 2020-02-18
        • 2018-12-17
        • 1970-01-01
        • 2017-06-05
        相关资源
        最近更新 更多