在Microsoft Azure通知中心上注册设备

时间:2018-06-08 20:11:34

标签: azure xamarin notifications azure-notificationhub

如何在Azure Notification Hub上注册令牌设备?我使用FCM,发送令牌。我的代码:

   public class AzureNotificationHubService
        {
            const string TAG = "AzureNotificationHubService";

            public static async Task RegisterAsync(Push push, string token)
            {
                try
                {
                    const string templateBody = "{\"data\":{\"message\":\"$(messageParam)\"}}";
                    JObject templates = new JObject();
                    templates["genericMessage"] = new JObject
                    {
                        {"body", templateBody}
                    };
                    await push.RegisterAsync(token, templates);
                    //await push.RegisterTemplateAsync(token, templateBody, TAG);
                    //await push.RegisterAsync(new Registration(token, new string[] { TAG }));

                    App.Auth.UpdatePushToken = token;
                }
                catch (Exception ex)
                {
                    Log.Error(TAG, "Could not register with Notification Hub: " + ex.Message);
                }
            }
        }

        // This service handles the device's registration with FCM.
        [Service]
        [IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
        public class FirebaseRegistrationService : FirebaseInstanceIdService
        {
            const string TAG = "FirebaseRegistrationService";

            public override void OnTokenRefresh()
            {
                var refreshedToken = FirebaseInstanceId.Instance.Token;
                Console.WriteLine($"Token received: {refreshedToken}");
                SendRegistrationTokenToAzureNotificationHub(refreshedToken);

                // await SendRegistrationToServerAsync(refreshedToken);
                //await SendRegistrationTokenToAzureNotificationHub(refreshedToken);
            }

            void SendRegistrationTokenToAzureNotificationHub(string token)
            {
                // Update notification hub registration
                Task.Run(async () =>
                {
                    //var client = new MobileServiceClient($"{MyResource.GetPushHost}/{token}");
                    //var push = client.GetPush();

                    await AzureNotificationHubService.RegisterAsync(push, token);
                });
            }
        }

方法 SendRegistrationTokenToAzureNotificationHub 我如何推送?在 AzureNotificationHubService.RegisterAsync 代码上等待push.RegisterAsync(token,templates);不行。救命啊!

0 个答案:

没有答案