2014-09-02 43 views
2

我正在使用xamarin处理iOS应用程序。现在我遇到了这个问题,我想将推送通知从服务器发送到连接的设备。当我的应用在后台运行时,我可以收到这些通知&。现在的问题是,我的应用程序被终止时是否可以收到通知? (主页按钮向上滑动)?我读过它,它应该是可能的,但不可靠。但是,我仍然如何做?应用程序被杀时无法接收APNS

我读通过launchoptions

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) 

得到通知,但它是空永诺&没有办法来调试它,因为我结束我的应用程序。 有关我在做什么错的任何想法?

P.S.我使用的是开发配置文件(也许这是什么问题?)

编辑1:额外的信息(代码)

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) 
    { 
     application.RegisterForRemoteNotificationTypes (UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound); 

     if (launchOptions != null) 
     { 
      // check for a local notification 
      if (launchOptions.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey)) 
      { 
       var localNotification = launchOptions[UIApplication.LaunchOptionsRemoteNotificationKey]; 
       if (localNotification != null) 
       { 

        new UIAlertView("TESTER", "TESTER3", null, "OK", null).Show(); 
        // reset our badge 
        UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0; 
       } 
      } 
     } 
     return true; 
    } 

    public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken) 
    { 
     Console.WriteLine (deviceToken.ToString()); 
     String tokenStr = deviceToken.Description; 
     var deviceToken1 = tokenStr.Replace ("<", "").Replace (">", "").Replace (" ", ""); 
     ViewController.deviceToken = deviceToken1; 
    } 

    public override void DidReceiveRemoteNotification (UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler) 
    { 
     Console.WriteLine ("Recieved"); 
     var message = userInfo.ValueForKey (new NSString ("aps")).ValueForKey (new NSString ("message")).ToString(); 
     var reload = userInfo.ValueForKey (new NSString ("aps")).ValueForKey (new NSString ("refreshList")).ToString(); 
     var vehicleId = userInfo.ValueForKey (new NSString ("aps")).ValueForKey (new NSString ("vehicleListId")).ToString(); 
    } 

==>做的东西与3个瓦尔(与信息)

回答

0

首先,可以确保所有的证书都是正确的(例如:使用沙箱服务器的开发证书,生产服务器的用户生产证书)

其次,您需要使用两种不同的方法处理推送通知。我不确定C#或Xamarin中的代码,Objective-C中的代码如下所示: -

//Receive Push Notification when the app is active in foreground or background 
- (void)application:(UIApplication *)application 
      didReceiveRemoteNotification:(NSDictionary *)userInfo { 

    if(userInfo){ 
    //TODO: Handle the userInfo here 
    } 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    //Get the push notification when app is not open 
    NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; 

    if(remoteNotif){ 
     [self handleRemoteNotification:application userInfo:remoteNotif]; 
    } 

    return YES; 
} 

-(void)handleRemoteNotification:(UIApplication*)application userInfo:(NSDictionary*)userInfo{ 

    if(userInfo){ 
     //TODO: Handle the userInfo here 
    } 
} 
+0

亲爱的,谢谢你的回答!我正在使用正确的证书,因为我可以在应用程序运行/背景时收到通知。到目前为止,我看到,我拥有相同的实现。 公共覆盖布尔FinishedLaunching(UIApplication的应用,NSDictionary的launchOptions) 如果(launchOptions!= NULL){// 做一些 &我有收到remoteNotification 公众覆盖无效DidReceiveRemoteNotification(UIApplication的应用程序,用户信息的NSDictionary,动作 completionHandler) – 2014-09-02 08:30:39

+0

hm ...如果你的实现与我的实现完全一样,那么我真的不知道问题出在哪里。您可能需要发布更多信息才能让其他人更好地理解您的问题。 – Ricky 2014-09-02 08:38:57

+0

我已经使用我目前拥有的代码编辑了我的问题。希望这个清除一点。 – 2014-09-02 10:48:55

相关问题