菜鸟教程小白 发表于 2022-12-13 11:19:08

c# - 没有设备注册 Parse Push 和 Xamarin IOS


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

<p> <a href="https://www.parse.com/docs/dotnet/guide#push-notifications-push-on-xamarin-ios" rel="noreferrer noopener nofollow">https://www.parse.com/docs/dotnet/guide#push-notifications-push-on-xamarin-ios</a>
<a href="https://www.parse.com/apps/quickstart#parse_push/ios/xamarin/existing" rel="noreferrer noopener nofollow">https://www.parse.com/apps/quickstart#parse_push/ios/xamarin/existing</a>
<a href="https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/remote_notifications_in_ios/" rel="noreferrer noopener nofollow">https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/remote_notifications_in_ios/</a>
<a href="https://groups.google.com/forum/#!topic/parse-developers/65WAfRIiEnA" rel="noreferrer noopener nofollow">https://groups.google.com/forum/#!topic/parse-developers/65WAfRIiEnA</a> </p>

<p>根据我对我的 AppDelegate 的修改的理解,</p>

<p>我在构造函数中添加了这个:</p>

<pre><code>ParseClient.Initialize(&#34;MY APP ID&#34;, &#34;MY DOT NET KEY&#34;);
</code></pre>

<p>使用适当的键。</p>

<p>我将它添加到 FinishedLaunching 方法中</p>

<pre><code>if (Convert.ToInt16(UIDevice.CurrentDevice.SystemVersion.Split(&#39;.&#39;).ToString()) &lt; 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) =&gt; {
      Utils.Log(&#34;Push!&#34;);
};
</code></pre>

<p>然后我添加了适当的覆盖:</p>

<pre><code>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(&#34;Error registering push notifications&#34;, error.LocalizedDescription, null, &#34;OK&#34;, null).Show();
}

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

<p>但还是没有运气。</p>

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

<p>我尝试过的事情:</p>

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

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

<p>Parse 为什么不同意?</p>

<p>我错过了什么?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我也遇到了同样的问题。我已经按照教程重新创建了配置文件、.p12 等,就像你一样。仍然没有运气。我按照这里的示例进行了手动操作 - <a href="https://www.parse.com/docs/rest/guide/#push-notifications-uploading-installation-data" rel="noreferrer noopener nofollow">https://www.parse.com/docs/rest/guide/#push-notifications-uploading-installation-data</a>并通过 curl 命令注册我的设备:</p>

<pre><code>curl -X POST \
-H &#34;X-Parse-Application-Id: &lt;Your Parse app ID&gt; &#34; \
-H &#34;X-Parse-REST-API-Key: &lt;Your parse REST API key&gt;&#34; \
-H &#34;Content-Type: application/json&#34; \
-d &#39;{
    &#34;deviceType&#34;: &#34;ios&#34;,
    &#34;deviceToken&#34;: &#34;&lt;Your device token&gt;&#34;,
    &#34;channels&#34;: [
      &#34;&#34;
    ]
}&#39; \
https://api.parse.com/1/installations
</code></pre>

<p>这样做后,我立即看到我的设备已注册。我尝试发送推送通知,但尚未收到。有些东西告诉我 Xamarin 安装或证书/配置文件存在问题。我还在努力解决这个问题,如果我解决了我会更新。</p>

<p>编辑 1:我发现如果您卸载正在使用的应用程序并重新安装,那么您的设备 token 会更改。无论如何,所以我再次尝试了一些不同的东西,我启动了我的应用程序并通过 xamarin 在 Debug模式下运行它。我捕获了新的设备 token 并再次发出了那个 curl 命令。 Parse 现在告诉我我注册了两个设备。我点击了我的应用程序上的主页按钮并告诉 parse 触发推送通知,我收到了它。所以现在我正在调查应用程序是否正确注册了 parse。它似乎试图进行通信,但某处可能出现错误,我不确定它可能是什么。</p>

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

<pre><code>public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken) {
      ParseObject obj = ParseObject.Create (&#34;_Installation&#34;);

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

<p>我还是想知道为什么我们不能这样做</p>

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

installation.SaveAsync();
}
</code></pre>

<p>但现在我有一些可行的方法。</p></p>
                                   
                                                <p style="font-size: 20px;">关于c# - 没有设备注册 Parse Push 和 Xamarin IOS,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33041335/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33041335/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: c# - 没有设备注册 Parse Push 和 Xamarin IOS