2016-07-19 64 views
1

我试图为我们的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文件),直到没有错误显示控制台。

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

我真的无能为力,应该继续寻找。

+0

手动从设备/模拟器中删除应用程序,然后允许它被xs/vs重新部署并重新测试 – SushiHangover

+0

感谢您的建议,我试过这样做,但即使这样做也没有改变。我在我的应用程序启动时显示“AppName想要显示通知”(或沿着这些行),但既不调用'RegisteredForRemoteNotifications'也不调用'FailedToRegisterForRemoteNotifications'。 –

+0

我想我发现了一些与此相同的问题,[在这里](http://stackoverflow.com/a/38454917/6470327) –

回答

1

浪费了几个小时今天要解决这个问题之后,我遇到了this answer,它引述:

长挖我发现,2016年7月19日后,由于一些错误或 更新用在Apple的结尾, didRegisterForRemoteNotificationsWithDeviceToken方法不会被 调用,即使每个条件如Internet连接,Device和使用的方法都是完美的。

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

要验证,请在你的其他应用程序看看了。我几小时浪费了我的 ,但希望它可以帮助某人。谢谢。

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


2016年7月20日编辑:
没有在我的代码改变任何东西,它开始通宵工作了。可以肯定的是,苹果已经解决了这个问题。

2

昨天我有同样的问题。今天,我再次尝试,没有任何改变我的代码,它的工作。我在这个问题上花了大约5个小时,甚至搜索了Apple开发者网站,检查Apple服务是否有问题,但没有发现任何问题。绝对烦人!

最后,它似乎是一个苹果引起的错误,今天已经解决。