注册远程通知会无声地失败

时间:2016-07-19 13:09:12

标签: xamarin xamarin.ios google-cloud-messaging

我正在尝试为我们的Xamarin.iOS应用实施推送通知。我已经关注了多个Getting Started guides和其他教程,但我似乎无法获得设备令牌。它以前工作过,没有什么重大改变,但我似乎无法在这里查明问题。

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)
    {
        // ...
    }
}

为简洁起见,我在某些方面缩短了代码。

在昨天的某个时刻,我正确地获得了我的令牌,甚至可以收到我应该得到的一些通知。在不改变FinishedLaunching方法的任何内容的情况下,它突然停止工作,没有任何关于原因的反馈。

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

我已多次阅读清理+重建我的项目/解决方案,我现在做了几次,但无济于事。

对于我应该继续寻找的地方,我真的很无能为力。

2 个答案:

答案 0 :(得分:2)

昨天我遇到了同样的问题。今天我再次尝试了它而没有对我的代码进行任何更改,并且它有效。我在这个问题上花了大约5个小时,甚至搜索了Apple开发者网站,检查Apple服务是否存在问题并且没有找到任何内容。绝对烦人!

最终它似乎是苹果造成的错误,今天已经解决了。

答案 1 :(得分:1)

今天浪费了几个小时试图修复此问题后,我遇到this answer,引用了:

  

经过长时间的挖掘后,我发现 2016年7月19日由于某些错误或   在Apple的最后更新,   didRegisterForRemoteNotificationsWithDeviceToken方法不会   即使每个条件,如互联网连接,设备和   使用的方法是完美的。

     

请参阅此链接进行确认   https://forums.developer.apple.com/thread/52224

     

要验证,请查看您的其他应用。我浪费了我的力量   几个小时,但希望它可以帮助某人。感谢。

我想我只能等到Apple解决这个问题。

编辑 2016年7月20日
在我的代码中没有改变任何内容的情可以安全地假设Apple已经解决了这个问题。