gpt4 book ai didi

android - Xamarin Android 中的设备 ID 意味着什么 Azure 推送通知?如何获得?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:02:00 26 4
gpt4 key购买 nike

我们正在使用 Azure 移动服务将通知推送到 Xamarin Android、Xamarin iOS 和 Windows 通用应用程序。 Windows 通用应用程序有大量关于我们需要的文档,尽管我们还没有机会实现它。但是,Xamarin Android 和 iOS 都缺少有关推送通知的所有文档。如果您转到http://azure.microsoft.com/en-us/documentation/services/mobile-services/并选择 Xamarin Android 或 Xamarin iOS 和 .NET 后端,有关这些 API 的文档的链接为零。昨天挖掘了大量内容后,我发现了这个:http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-xamarin-android-get-started-push/http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-xamarin-ios-get-started-push/两者最后一次更新都是在去年九月。该文档在 5 个多月前就 promise 更新。

当我将 Microsoft 的 Xamarin 组件用于 Azure 移动服务时:http://components.xamarin.com/view/azure-mobile-services/我能够启动并运行 MobileServiceClient,但无法启动推送通知。

API:

Push pushManager = MobileService.GetPush();
string deviceId = "what is this???";
//Option 1:
pushManager.RegisterNativeAsync(deviceId);
//Option 2:
GcmRegistration googleNotificationRegistration = new GcmRegistration(deviceId);
pushManager.RegisterAsync(googleNotificationRegistration);

我正在使用的文档:

我的问题很简单:deviceId 应该是什么?我怎样才能得到它?

以上所有文档均适用于 Winodws 通用应用程序,不适用于 Mono 上的 Xamarin 应用程序。

在撰写此问题时,我找到了有关“通知中心入门”的文章:

这些是我应该使用的示例吗?它们看起来很旧,而且 Android 版本没有提及 Azure 移动服务。我什至不应该使用适用于 Android 的 Azure 移动服务 Xamarin 组件吗?

最佳答案

tl;博士

deviceId 应该只是 GCMRegistrationId

<小时/>

我研究了组件 DLL 和 Android SDK 实现的源代码。

首先,让我们看看幕后的选项 1选项 2。基本上,两者最终都会执行相同的工作:创建 GcmRegistration 并将其传递给内部 RegistrationManager

    public Task RegisterAsync (Registration registration)
{
if (registration == null) {
throw new ArgumentNullException ("registration");
}
if (string.IsNullOrWhiteSpace (registration.PushHandle)) {
throw new ArgumentNullException ("registration.deviceId");
}
return this.RegistrationManager.RegisterAsync (registration);
}

public Task RegisterNativeAsync (string deviceId, IEnumerable<string> tags)
{
if (string.IsNullOrWhiteSpace (deviceId)) {
throw new ArgumentNullException ("deviceId");
}
GcmRegistration registration = new GcmRegistration (deviceId, tags);
return this.RegistrationManager.RegisterAsync (registration);
}

然后,我可以找到涉及 Registration.PushHandle (即您传递的 deviceId)的 API 调用之一,如下所示下面

    public async Task<IEnumerable<Registration>> ListRegistrationsAsync (string deviceId)
{
MobileServiceHttpResponse mobileServiceHttpResponse = await this.client.HttpClient.RequestAsync (HttpMethod.Get, string.Format ("/push/registrations?deviceId={0}&platform={1}", new object[] {
Uri.EscapeUriString (deviceId),
Uri.EscapeUriString (Platform.Instance.PushUtility.GetPlatform ())
}), this.client.CurrentUser, null, true, null, MobileServiceFeatures.None);
return JsonConvert.DeserializeObject<IEnumerable<Registration>> (mobileServiceHttpResponse.Content, new JsonConverter[] {
new RegistrationConverter ()
});
}

然后我切换到 Android Mobile Services SDK 来寻找类似的代码来找到一些提示。遗憾的是,在 android 中发现它名为 pnsHandle 但仍然没有提示它是什么。

    /**
* Registers the client for native notifications with the specified tags
* @param pnsHandle PNS specific identifier
* @param tags Tags to use in the registration
* @return The created registration
* @throws Exception
*/
public Registration register(String pnsHandle, String... tags) throws Exception {
if (isNullOrWhiteSpace(pnsHandle)) {
throw new IllegalArgumentException("pnsHandle");
}

Registration registration = PnsSpecificRegistrationFactory.getInstance().createNativeRegistration(mNotificationHubPath);
registration.setPNSHandle(pnsHandle);
registration.setName(Registration.DEFAULT_REGISTRATION_NAME);
registration.addTags(tags);

return registerInternal(registration);
}

最后,我猜下面的示例代码来自 http://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-android-get-started-push/#update-app应该调用相同的 API,它现在可以解释所有内容,即 deviceId 只是 GCMRegistrationId

    @Override
public void onRegistered(Context context, final String gcmRegistrationId) {
super.onRegistered(context, gcmRegistrationId);

new AsyncTask<Void, Void, Void>() {

protected Void doInBackground(Void... params) {
try {
ToDoActivity.mClient.getPush().register(gcmRegistrationId, null);
return null;
}
catch(Exception e) {
// handle error
}
return null;
}
}.execute();
}

关于android - Xamarin Android 中的设备 ID 意味着什么 Azure 推送通知?如何获得?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28839168/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com