OGeek|极客世界-中国程序员成长平台

标题: c# - 没有设备注册 Parse Push 和 Xamarin IOS [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:19
标题: c# - 没有设备注册 Parse Push 和 Xamarin IOS

我正在尝试使用 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("ush!");
};

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

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 方法中添加了一个断点,它确实被调用了,所以我显然是在注册通知,但是当我尝试从 parse 发送推送通知时,它告诉我“没有注册设备”。

我尝试过的事情:

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

Parse 为什么不同意?

我错过了什么?



Best Answer-推荐答案


我也遇到了同样的问题。我已经按照教程重新创建了配置文件、.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:我发现如果您卸载正在使用的应用程序并重新安装,那么您的设备 token 会更改。无论如何,所以我再次尝试了一些不同的东西,我启动了我的应用程序并通过 xamarin 在 Debug模式下运行它。我捕获了新的设备 token 并再次发出了那个 curl 命令。 Parse 现在告诉我我注册了两个设备。我点击了我的应用程序上的主页按钮并告诉 parse 触发推送通知,我收到了它。所以现在我正在调查应用程序是否正确注册了 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();
}

但现在我有一些可行的方法。

关于c# - 没有设备注册 Parse Push 和 Xamarin IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041335/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4