Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 远程通知在应用程序位于前台而不是后台时工作_C#_Ios_Iphone_Xamarin.ios_Apple Push Notifications - Fatal编程技术网

C# 远程通知在应用程序位于前台而不是后台时工作

C# 远程通知在应用程序位于前台而不是后台时工作,c#,ios,iphone,xamarin.ios,apple-push-notifications,C#,Ios,Iphone,Xamarin.ios,Apple Push Notifications,我有一个Xamarin.iOS应用程序正在运行,当应用程序在前台运行时,它可以成功接收APNS推送通知。但是,我也希望能够在应用程序在后台运行或根本不运行时处理推送通知 关于Xamarin的这一页似乎表明,您确实可以做到这一点: 我正在使用上面概述的远程通知策略。所以我实现了它所说的一切,包括将info.plist更改为具有远程通知作为后台操作。但是,当应用程序在后台运行时,我在DidFinishedLaunching或ReceivedRemoteNotification回调中都没有收到任何呼

我有一个Xamarin.iOS应用程序正在运行,当应用程序在前台运行时,它可以成功接收APNS推送通知。但是,我也希望能够在应用程序在后台运行或根本不运行时处理推送通知

关于Xamarin的这一页似乎表明,您确实可以做到这一点:

我正在使用上面概述的远程通知策略。所以我实现了它所说的一切,包括将info.plist更改为具有远程通知作为后台操作。但是,当应用程序在后台运行时,我在DidFinishedLaunching或ReceivedRemoteNotification回调中都没有收到任何呼叫。当应用程序在前台运行时,一切正常。不用说,这太令人沮丧了

还有其他人遇到过这个问题吗

非常感谢您的帮助

以下是我用于注册远程通知的代码:

    public static void NotifyAppStart()
    {
        if (GetRemoteWipePreference())
        {
            if (Utils.CheckOsVersion(8, 0))
            {
                // iOS 8 version

                UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet());  

                UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);

                UIApplication.SharedApplication.RegisterForRemoteNotifications();

            }
            else
            {
                // Only works on iOS 7 and earlier

                UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
                UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
            }


        }

    }
下面是我用来在服务器端发送推送通知的PushSharp代码:

push.RegisterAppleService(new ApplePushChannelSettings(appleCert, GetApplePwd()  ));
        push.QueueNotification(new AppleNotification()
                                   .ForDeviceToken( token ).WithContentAvailable(1)
                                   .WithCustomItem(msg, device.device_local_guid)  );
旁注:关于堆栈溢出,还有一些其他问题与此有关,但没有一个是关于此问题的

if (UIDevice.CurrentDevice.CheckSystemVersion(8,0))
        {
            var settings = UIUserNotificationSettings.GetSettingsForTypes (UIUserNotificationType.Sound |
                UIUserNotificationType.Alert | UIUserNotificationType.Badge, null);

            UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
            UIApplication.SharedApplication.RegisterForRemoteNotifications ();
        }
        else
        {
            UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.Badge |
                UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert);
        }
你也需要这个

public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
    {
        var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");

        var strFormat = new NSString("%@");
        var dt = new NSString(MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend_IntPtr_IntPtr(new MonoTouch.ObjCRuntime.Class("NSString").Handle, new MonoTouch.ObjCRuntime.Selector("stringWithFormat:").Handle, strFormat.Handle, deviceToken.Handle));
        var newDeviceToken = dt.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
        string sql;

        if (string.IsNullOrEmpty(oldDeviceToken) || !deviceToken.Equals(newDeviceToken))
        {
            do someting
        }

        if (oldDeviceToken == newDeviceToken) 
        {
            do someting
        } else 
        {
            do something
        }

        NSUserDefaults.StandardUserDefaults.SetString(newDeviceToken, "PushDeviceToken");
        Console.WriteLine("Device Token: " + newDeviceToken);
    }

您的json文件是否包含可用的
内容
布尔键为“false”? 例如:

{
   "to": "GCM Registration ID",
  "priority":"high",
  "data": {
        "notificationId":123,
        "title":"Spot you!",
        "message": "&lt;font color=\"red\"&gt;&lt;b&gt;Test RED Bold text maybe ?&lt;/b&gt;&lt;/font&gt;&lt;br/&gt;Normal second line.&lt;br/&gt;&lt;br/&gt;",
        "photoUrl": "http://babymetal.net/wp-content/uploads/2015/07/ladybeard-e1427784688234.jpg",
        "badge": 1
  },
   "dry_run" : false,       
   "time_to_live" : 4,
   "content_available" : false
}
上面的json返回应该调用您的方法

public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)

你在模拟器中测试吗?如果是的话,SDK8模拟器中有一个bug,它阻止了应用程序真正的后台运行。没有-在设备上测试(iPhone 6)嗨,谢谢你的反馈-问题是我试图让它在后台运行时工作,而不是在前台。你知道怎么做吗?
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)