2015-07-05 43 views
1

我試圖在Xamarin形式的IOS中實現PCL應用程序中的推送通知。我有一個合適的預配置配置文件和p12文件,我用它將通知發送到本地應用程序,並將其用於Xamarin表單上,我是否需要在此更改旁邊進行更多操作?我改變了的AppDelegate在IOS項目看起來像這樣:以Xamarin形式推送通知

using System; 
using System.Collections.Generic; 
using System.Linq; 

using Foundation; 
using UIKit; 

namespace Punteam.iOS 
{ 
[Register ("AppDelegate")] 
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate 
{ 
    public override bool FinishedLaunching(UIApplication app, NSDictionary options) 
    { 

     String model = UIDevice.CurrentDevice.Model; 
     String sysName = UIDevice.CurrentDevice.SystemName; 
     String sysVer = UIDevice.CurrentDevice.SystemVersion; 
     NSUserDefaults.StandardUserDefaults.SetString (model, "Model"); 
     NSUserDefaults.StandardUserDefaults.SetString (sysName, "sysName"); 
     NSUserDefaults.StandardUserDefaults.SetString (sysName, "sysVer"); 

     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); 
    } 
    //*********************************************************************************************** 
    //** RegisteredForRemoteNotifications               * 
    //*********************************************************************************************** 

    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"); 
    } 
    //*********************************************************************************************** 
    //** ReceivedRemoteNotification                 * 
    //*********************************************************************************************** 

    public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo) 
    { 
     ProcessNotification(userInfo, false); 
    } 
    //*********************************************************************************************** 
    //** ProcessNotification                  * 
    //*********************************************************************************************** 

    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 alertString = string.Empty; 
      string paramString = string.Empty; 

      if (aps.ContainsKey(new NSString("alert"))) 
       alertString = (aps[new NSString("alert")] as NSString).ToString(); 

      if (aps.ContainsKey(new NSString("param"))) 
       paramString = (aps[new NSString("param")] as NSString).ToString(); 

      if (!fromFinishedLaunching) 
      { 
       //Manually show an alert 
       if (!string.IsNullOrEmpty(alertString)) 
       { 
        UIAlertView avAlert = new UIAlertView("Awesome Notification", alertString , null, 
         NSBundle.MainBundle.LocalizedString("Cancel", "Cancel"), 
         NSBundle.MainBundle.LocalizedString("OK", "OK")); 

        avAlert.Clicked += (sender, buttonArgs) => 
        { 
         if (buttonArgs.ButtonIndex != avAlert.CancelButtonIndex) 
         { 
          if (!string.IsNullOrEmpty(paramString)) 
          { 
//        App.Current.MainPage = new NavigationPage(new PushNotifMessageDisplay(paramString)); 
          } 
         } 
        }; 

        avAlert.Show(); 
       } 
      } 
     } 
    } 
    //*********************************************************************************************** 
    //** FailedToRegisterForRemoteNotifications              * 
    //*********************************************************************************************** 

    public override void FailedToRegisterForRemoteNotifications (UIApplication application , NSError error) 
    { 
     new UIAlertView("Error registering push notifications", error.LocalizedDescription, null, "OK", null).Show(); 
    } 
} 
} 

回答

1

您可以檢查我的完整的例子推送通知here

關於iOS的一部分,這是相關的代碼,去你的AppDelegate

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) 
{ 
    var deviceTokenString = deviceToken.ToString().Replace("<","").Replace(">", "").Replace(" ", ""); 
    var notificationService = Resolver.Resolve<IPushNotificationService>(); 
    var pushNotificationRegister = Resolver.Resolve<IPushNotificationRegister>(); 

    if (pushNotificationRegister.ShouldSendToken(deviceTokenString)) 
    { 
     var uid = UIDevice.CurrentDevice.IdentifierForVendor.AsString(); 
     notificationService.AddPushToken(deviceTokenString, DeviceUtils.GetDeviceType(), uid); 
    } 
} 

* IPushNotificationRegister - 如果令牌已經被髮送到服務器(這是爲了避免unneede檢查d請求到服務器)。

* IPushNotificationService - 將令牌發送到服務器的服務。

+0

謝謝! IdiT,你已經回答了,關於Xamarin的主要問題,再次感謝 –