简体   繁体   English

Android上的Azure通知中心。 如何发送给特定用户?

[英]Azure Notification Hub on Android. How to send to specific user?

I want to create a little application to send notification to my Android Device. 我想创建一个小应用程序以将通知发送到我的Android设备。

I made a WPF Application and an Android Application and I use Azure Notification Hub and GCM for notif. 我制作了WPF应用程序和Android应用程序,并使用Azure Notification Hub和GCM进行通知。

I can send a notification for all my android device but not for ONE device. 我可以为我所有的android设备发送通知,但不能为一个设备发送通知。

And i come here to get your help to do this. 我来这里是为了得到您的帮助。

My WPF Application work like this : 我的WPF应用程序是这样的:

 private async void SendNotificationAsync()
    { 
        var hub = NotificationHubClient.CreateClientFromConnectionString(Keys.FullConnectionString, Keys.NotificationHubName);
        if (!string.IsNullOrEmpty(TxtTo.Text)) 
            await hub.SendGcmNativeNotificationAsync(
                    "{'to': '" + TxtTo.Text + "', 'data': { 'message': '" + TxtMessage.Text + "', 'title': '" + TxtTitle.Text + "' } }");
        else
        {
            await hub.SendGcmNativeNotificationAsync(
                    "{'data': { 'message': '" + TxtMessage.Text + "', 'title': '" + TxtTitle.Text + "' } }");
        }
    }

My problem is this: 我的问题是这样的:

I don't understand how to get the Android ID Device Registration, or then, it's my RegistrationId generated with my OnRegistered method : 我不知道如何获取Android ID设备注册,否则,这是使用OnRegistered方法生成的我的RegistrationId:

 protected override void OnRegistered(Context context, string registrationId)
    {
        Log.Verbose(MyBroadcastReceiver.TAG, "GCM Registered: " + registrationId);

        RegistrationId = registrationId;
        ChangeTextID(registrationId);

        Hub = new NotificationHub(Keys.NotificationHubName, Keys.ListenConnectionString, context);

        try
        {
            Hub.UnregisterAll(registrationId);
        }
        catch (Exception ex)
        {
            Log.Error(MyBroadcastReceiver.TAG, ex.Message);
        }

        var tags = new List<string>();

        try
        {
            Hub.Register(registrationId, tags.ToArray());
        }
        catch (Exception ex)
        {
            Log.Error(MyBroadcastReceiver.TAG, ex.Message);
        }
    }

But if i use this ID to send my notification (in my WPF app) it dosen't work (Replace TxtTo.Text) 但是,如果我使用此ID发送通知(在我的WPF应用程序中),则该通知不起作用(替换TxtTo.Text)

Can you help me? 你能帮助我吗?

You do not need to use anything for sending targeted push notification except tag you subscribed that application to. 除了订阅该应用程序的标签,您无需使用其他任何东西来发送定向的推送通知。 When you register the application for the notification hub, it will put its record into the notification hub backend registrations table (which you may access to from Azure Portal or using Service Bus Explorer . If you subscribed your application to a tag, and send the notification to that tag, and application did not receive it, then see if you have that application in the table with the appropriate tag. 当您为通知中心注册应用程序时,它将记录记录到通知中心后端注册表中(您可以从Azure门户或使用Service Bus Explorer访问该表。如果您将应用程序订阅到标签,然后发送通知到该标签,并且应用程序未收到它,然后查看表中是否有带有相应标签的应用程序。

That is the great feature because you may send targeted notifications to users without knowing their IDs or whatever. 那是个很棒的功能,因为您可能在不知道用户ID或其他信息的情况下向他们发送定向通知。 Just subscribe them to that tag. 只需为他们订阅该标签即可。 That works perfectly among platforms - we use that approach to send targeted notifications for users subscribed to the same tag on different devices and platforms. 在平台之间完美搭配-我们使用这种方法为在不同设备和平台上订阅了相同标签的用户发送定向通知。

May be relevant. 可能是相关的。 As i see, you send your notification not to a tag (try to add to SendGcmNativeNotificationAsync extra argument like here ). 依我之见,你把你的通知,而不是一个标签(尝试添加到SendGcmNativeNotificationAsync额外的参数喜欢这里 )。 Try to send it to the exact tag (you can do that from a portal, Notification Hub dashboard => DEBUG pane). 尝试将其发送到确切的标签(您可以从门户网站的Notification Hub仪表板=> DEBUG窗格中执行此操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM