应用被杀死时无法接收APNS

时间:2014-09-02 07:53:01

标签: c# ios xamarin apple-push-notifications

我正在使用xamarin开发iOS应用。现在我遇到了这个问题,我想将推送通知从服务器发送到连接的设备。当我的应用运行时,我可以收到这些通知&当我的应用程序在后台时。现在的问题是,当我的应用程序被杀死时,我可以收到通知吗? (主页按钮向上滑动)?我读到了它,它应该是可能的但不可靠。但是,我该怎么做呢?

我读过有关通过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 ();
    }

==&GT;用3个vars(带信息)做东西

1 个答案:

答案 0 :(得分: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
    }
}