应用程序被终止时无法接收APNS

本文关键字:APNS 终止 应用程序 | 更新日期: 2023-09-27 18:20:22

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

我读到关于通过启动获得通知的消息

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)

但它总是空的&由于我正在关闭我的应用程序,因此无法调试它。有没有想过我做错了什么?

附言:我正在使用开发配置文件(也许这是问题所在?)

编辑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个变量(带信息)

应用程序被终止时无法接收APNS

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

其次,您需要用两种不同的方法来处理推送通知。我不确定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
    }
}