我试图为我们的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
文件),直到没有错误显示控制台。
我已经多次阅读清理+重建我的项目/解决方案,我已经做了几次,但无济于事。
我真的无能为力,应该继续寻找。
手动从设备/模拟器中删除应用程序,然后允许它被xs/vs重新部署并重新测试 – SushiHangover
感谢您的建议,我试过这样做,但即使这样做也没有改变。我在我的应用程序启动时显示“AppName想要显示通知”(或沿着这些行),但既不调用'RegisteredForRemoteNotifications'也不调用'FailedToRegisterForRemoteNotifications'。 –
我想我发现了一些与此相同的问题,[在这里](http://stackoverflow.com/a/38454917/6470327) –