Xamarin 注册远程通知会以静默方式失败

Xamarin 注册远程通知会以静默方式失败,xamarin,xamarin.ios,google-cloud-messaging,Xamarin,Xamarin.ios,Google Cloud Messaging,我正在尝试为我们的Xamarin.iOS应用程序实现推送通知。我已经学习了多个教程和其他教程,但我似乎再也找不到设备令牌了。它以前是有效的,没有什么显著的变化,但我似乎无法在这里指出问题所在 AppDelegate.cs [Register("AppDelegate")] public class AppDelegate : UIApplicationDelegate, IReceiverDelegate { public override bool FinishedLaunching(

我正在尝试为我们的Xamarin.iOS应用程序实现推送通知。我已经学习了多个教程和其他教程,但我似乎再也找不到设备令牌了。它以前是有效的,没有什么显著的变化,但我似乎无法在这里指出问题所在

AppDelegate.cs

[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate, IReceiverDelegate
{
    public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
    {
        var gcmConfig = Google.GoogleCloudMessaging.Config.DefaultConfig;
        gcmConfig.ReceiverDelegate = this;
        Google.GoogleCloudMessaging.Service.SharedInstance.Start(gcmConfig);

        var notTypes = UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge;
        var settings = UIUserNotificationSettings.GetSettingsForTypes(notTypes, null);
        UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
        UIApplication.SharedApplication.RegisterForRemoteNotifications();

        // ... Other application stuff

        return true;
    }

    public override void OnActivated(UIApplication application)
    {
        Google.GoogleCloudMessaging.Service.SharedInstance.Connect(error =>
        {
            if (error != null)
            {
                Console.WriteLine("GCM Connect error: " + error);
            }
        });
    }

    public override void DidEnterBackground(UIApplication application)
    {
        Google.GoogleCloudMessaging.Service.SharedInstance.Disconnect();
    }

    private NSData DeviceToken;
    // This function does NOT get called anymore
    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        this.DeviceToken = deviceToken;

        var config = Google.InstanceID.Config.DefaultConfig;
        Google.InstanceID.InstanceId.SharedInstance.Start(config);

        var options = new NSMutableDictionary();
        options.SetValueForKey(DeviceToken, Google.InstanceID.Constants.RegisterAPNSOption);
        options.SetValueForKey(new NSNumber(true), Google.InstanceID.Constants.APNSServerTypeSandboxOption);

        Google.InstanceID.InstanceId.SharedInstance.Token(
            GCMConnection.SenderId,
            Google.InstanceID.Constants.ScopeGCM,
            options,
            (token, error) => {
                if (error == null)
                {
                    // ... Send token to our API

                    PubSub.SharedInstance.Subscribe(token, "/topics/global", new NSDictionary(), delegate { });
                    return;
                }
                Console.WriteLine("Error getting GCM token: " + error);
                // Handle error
        });
    }

    public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
        Console.WriteLine(error);
    }

    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        // Handle notification here
        Google.GoogleCloudMessaging.Service.SharedInstance.AppDidReceiveMessage(userInfo);
    }


    [Export("didDeleteMessagesOnServer")]
    public void DidDeleteMessagesOnServer()
    {
        // ...
    }

    [Export("didSendDataMessageWithID:")]
    public void DidSendDataMessage(string messageID)
    {
        // ...
    }

    [Export("willSendDataMessageWithID:error:")]
    public void WillSendDataMessage(string messageID, NSError error)
    {
        // ...
    }
}
[注册(“AppDelegate”)]
公共类AppDelegate:UIApplicationDelegate、IReceiverDelegate
{
公共覆盖bool FinishedLaunching(UIApplication应用程序、NSDictionary启动选项)
{
var gcmConfig=Google.GoogleCloudMessaging.Config.DefaultConfig;
gcmConfig.ReceiverDelegate=此;
Google.GoogleCloudMessaging.Service.SharedInstance.Start(gcmConfig);
var notTypes=UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge;
var settings=UIUserNotificationSettings.GetSettingsForTypes(notTypes,null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(设置);
UIApplication.SharedApplication.RegisterForRemotonifications();
//…其他应用程序材料
返回true;
}
已激活公共覆盖无效(UIApplication)
{
Google.GoogleCloudMessaging.Service.SharedInstance.Connect(错误=>
{
if(错误!=null)
{
Console.WriteLine(“GCM连接错误:+错误”);
}
});
}
公共覆盖无效DiEnterBackground(UIApplication)
{
Google.GoogleCloudMessaging.Service.SharedInstance.Disconnect();
}
专用NSData DeviceToken;
//此函数不再被调用
公共覆盖无效注册更正(UIApplication应用程序,NSData deviceToken)
{
this.DeviceToken=DeviceToken;
var config=Google.InstanceID.config.DefaultConfig;
Google.InstanceID.InstanceID.SharedInstance.Start(配置);
var options=new NSMutableDictionary();
options.SetValueForKey(DeviceToken,Google.InstanceID.Constants.RegisterAppnSoption);
options.SetValueForKey(新的NSNumber(true),Google.InstanceID.Constants.APNSServerTypeSandboxOption);
Google.InstanceID.InstanceID.SharedInstance.Token(
GCMConnection.SenderId,
Google.InstanceID.Constants.ScopeGCM,
选项,
(令牌,错误)=>{
如果(错误==null)
{
//…将令牌发送到我们的API
Subscribe(令牌,“/topics/global”,new-NSDictionary(),委托{});
返回;
}
Console.WriteLine(“获取GCM令牌时出错:+错误”);
//处理错误
});
}
远程通知的公共覆盖无效注册失败(UIApplication应用程序,N错误)
{
控制台写入线(错误);
}
public override void DidReceiveEmotentification(UIApplication应用程序、NSDictionary userInfo、Action completionHandler)
{
//在这里处理通知
Google.GoogleCloudMessaging.Service.SharedInstance.AppDidReceiveMessage(userInfo);
}
[导出(“didDeleteMessagesOnServer”)]
public void DidDeleteMessagesOnServer()
{
// ...
}
[导出(“didSendDataMessageWithID:”)]
public void DidSendDataMessage(字符串messageID)
{
// ...
}
[导出(“willSendDataMessageWithID:error:”)]
public void WillSendDataMessage(字符串messageID,n错误)
{
// ...
}
}
为了简洁起见,我在某些方面缩短了代码

在昨天的某个时候,我得到了我的代币,甚至可以收到一些我应该得到的通知。没有对
FinishedLaunching
方法进行任何更改,它突然停止工作,没有任何关于原因的反馈

当我试图修复它时,我注意到Console.Log中与GCM相关的一些错误,我已经修复了这些错误(例如,我忘记了
GoogleService Info.plist
文件),以至于控制台中没有显示错误

我已经阅读了很多次来清理并重建我的项目/解决方案,我已经做了几次了,但都没有用


我真的不知道我应该继续寻找什么地方。

在今天花了几个小时试图解决这个问题后,我遇到了,其中引用了:

经过长时间的挖掘,我发现2016年7月19日,由于一些错误或 苹果端的更新 无法使用didRegisterForRemoteNotificationsWithDeviceToken方法 即使在任何情况下,如互联网连接、设备和 使用的方法是完美的

请参阅此链接进行确认

要验证,请查看您的其他应用程序。我浪费了我的时间 几个小时,但希望它能帮助别人。谢谢

我想我只能等到苹果解决这个问题了


2016年7月20日编辑:

在我的代码中没有任何更改,它在一夜之间又开始工作了。可以肯定的是,苹果已经解决了这个问题。

在今天花了几个小时试图解决这个问题之后,我遇到了一个问题,上面引用了:

经过长时间的挖掘,我发现2016年7月19日,由于一些错误或 苹果端的更新 无法使用didRegisterForRemoteNotificationsWithDeviceToken方法 即使在任何情况下,如互联网连接、设备和 使用的方法是完美的

请参阅此链接进行确认

要验证,请查看您的其他应用程序。我浪费了我的时间 几个小时,但希望它能帮助别人。谢谢

我想我只能等到苹果解决这个问题了


2016年7月20日编辑:
在我的代码中没有任何更改,它在一夜之间又开始工作了。