没有使用Parse Push和Xamarin IOS注册的设备

时间:2015-10-09 14:53:17

标签: c# ios parse-platform xamarin apple-push-notifications

我正在尝试使用Parse Push和Xamarin IOS设置推送通知。我遵循了这些指南:

https://www.parse.com/docs/dotnet/guide#push-notifications-push-on-xamarin-ios https://www.parse.com/apps/quickstart#parse_push/ios/xamarin/existing https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/remote_notifications_in_ios/ https://groups.google.com/forum/#!topic/parse-developers/65WAfRIiEnA

根据我对那些我修改AppDelegate的理解,

我将其添加到构造函数中:

ParseClient.Initialize("MY APP ID", "MY DOT NET KEY");  

使用适当的键。

我将此添加到FinishedLaunching方法

if (Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split('.')[0].ToString()) < 8) {
        UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
        UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
} else {
    UIUserNotificationType notificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
    var settings = UIUserNotificationSettings.GetSettingsForTypes(notificationTypes, new NSSet(new string[] { }));
    UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
    UIApplication.SharedApplication.RegisterForRemoteNotifications();
}

// Handle Push Notifications
ParsePush.ParsePushNotificationReceived += (object sender, ParsePushNotificationEventArgs args) => {
        Utils.Log("Push!");
};

然后我添加了适当的覆盖:

public override void DidRegisterUserNotificationSettings(UIApplication application,
    UIUserNotificationSettings notificationSettings) {
    application.RegisterForRemoteNotifications();
}

public override void RegisteredForRemoteNotifications(UIApplication application,
    NSData deviceToken) {
    ParseInstallation installation = ParseInstallation.CurrentInstallation;
    installation.SetDeviceTokenFromData(deviceToken);
    installation.SaveAsync();

}

public override void FailedToRegisterForRemoteNotifications (UIApplication application , NSError error)
{
    new UIAlertView("Error registering push notifications", error.LocalizedDescription, null, "OK", null).Show();
}

public override void ReceivedRemoteNotification(UIApplication application,
    NSDictionary userInfo) {
    // We need this to fire userInfo into ParsePushNotificationReceived.
    ParsePush.HandlePush(userInfo);
} 

但仍然没有运气。

我在RegisteredForRemoteNotifications方法中添加了一个断点,它确实被调用,因此我显然正在注册通知但是当我尝试从解析发送推送通知时它告诉我&#34;没有设备注册&#34;。

我尝试过的事情:

  • 检查配置文件是否已设置为推送通知。
  • 删除远程和本地的所有配置文件,然后重新生成它们。
  • 确保构建使用新的配置文件。
  • 重新生成.p12文件并重新上传到解析。
  • 在Info.plist和prov配置文件中检查我的包标识符是否一致。

但我认为这不是配置文件的问题,因为应用程序正在注册通知,

为什么Parse不同意?

我错过了什么?

1 个答案:

答案 0 :(得分:1)

我也经历过同样的问题。我按照教程并重新创建了配置文件,.p12等,就像你一样。仍然没有运气。我按照这里的示例 - https://www.parse.com/docs/rest/guide/#push-notifications-uploading-installation-data进行了手动操作,并通过curl命令注册了我的设备:

curl -X POST \
-H "X-Parse-Application-Id: <Your Parse app ID> " \
-H "X-Parse-REST-API-Key: <Your parse REST API key>" \
-H "Content-Type: application/json" \
-d '{
    "deviceType": "ios",
    "deviceToken": "<Your device token>",
    "channels": [
      ""
    ]
  }' \
https://api.parse.com/1/installations

完成后我立即看到我的设备已注册。我试图发送推送通知但尚未收到。有些东西告诉我Xamarin安装或证书/配置文件有问题。我还在努力,如果我解决它,我会更新。

编辑1:我发现如果您卸载正在处理的应用并重新安装,那么您的设备令牌会发生变化。无论如何所以我再次尝试了不同的东西,我启动了我的应用程序并通过xamarin以调试模式运行它。我抓住了新的设备令牌并再次发出了curl命令。 Parse现在告诉我我注册了两台设备。我点击我的应用程序上的主页按钮并告诉解析启动推送通知我收到它。所以现在我正在调查应用程序是否正在使用解析进行正确注册。它似乎试图沟通,但某处可能存在错误,我不确定它可能是什么。

更新:所以我终于让我的设备注册,但采用了完全不同的方式。这就是我的RegisteredForRemoteNotifications现在的样子。 (注意:我正在使用Parse 1.5.5,因为在撰写本文时,新版本不起作用。目前最新版本是1.6.2。

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {
        ParseObject obj = ParseObject.Create ("_Installation");

        string dt = deviceToken.ToString ().Replace ("<", "").Replace (">", "").Replace (" ", "");
        obj["deviceToken"] = dt;
        obj.SaveAsync ().ContinueWith (t => {
            if (t.IsFaulted) {
                using (IEnumerator<System.Exception> enumerator = t.Exception.InnerExceptions.GetEnumerator()) {
                    if (enumerator.MoveNext()) {
                        ParseException error = (ParseException) enumerator.Current;
                        Console.WriteLine ("ERROR!!!: " + error.Message);
                    }
                }
            } else {
                Console.WriteLine("Saved/Retrieved Installation");
                var data = NSUserDefaults.StandardUserDefaults;
                data.SetString ("currentInstallation", obj.ObjectId);
                Console.WriteLine("Installation ID = " + obj.ObjectId);
            }
        });
    }

我仍然想知道为什么我们不能做

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {
  ParseInstallation installation = ParseInstallation.CurrentInstallation;
  installation.SetDeviceTokenFromData(deviceToken);

  installation.SaveAsync();
}

但是现在我有一些有用的东西。