【问题标题】:What does Device Id mean Azure Push Notifications in Xamarin Android? How to get it?设备 ID 是什么意思 Xamarin Android 中的 Azure 推送通知?如何得到它?
【发布时间】:2015-05-04 12:56:05
【问题描述】:

我们正在使用 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/ 两者最后一次更新是在去年 9 月。文档承诺会在 5 个多月前更新。

当我将 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 组件吗?

【问题讨论】:

    标签: android ios azure xamarin push-notification


    【解决方案1】:

    tl;dr

    deviceId 应该只是 GCMRegistrationId


    我查看了组件 DLL 和 Android SDK 实现的源代码。

    首先,我们来看看你的option 1option 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 移动服务 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();
        }
    

    【讨论】:

    • 经过大量搜索并直接与 MS 团队成员交谈后,我同意。这正是 deviceId 应该是的。只是不清楚。在 Apple 方面,它在文档中更有意义。在编写了解决方案和数小时的研究之后,Android 方面也很有意义,只是不太清楚。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-11-12
    • 2017-03-01
    • 2015-02-09
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多