【问题标题】:Azure Notification Hubs with Android - Sending notifications to subsets of users带有 Android 的 Azure 通知中心 - 向部分用户发送通知
【发布时间】:2016-07-31 20:16:12
【问题描述】:

我目前正在构建一个 Android 消息传递应用程序,并且我正在尝试向属于某个组的所有用户发送通知。我设置了一个 Azure 通知中心,它可以很好地向所有已注册的设备发送通知,但我似乎无法让它仅适用于所有用户的一个子集,即一个组。

设备在启动时向 Azure 和 GCM 注册。

我曾尝试使用“标签”尝试向个人发送通知,但我不确定我是否做得对……我一定不是,因为它不起作用!

在下面的代码中,我试图通过使用他们的用户名作为标签向个人发送通知...

这是我的服务中发送通知的代码:

    // POST api/notification
    public async Task<IHttpActionResult> Post([FromBody]Notification notification)
    {
        var notificationToSave = new Notification
        {
            NotificationGuid = Guid.NewGuid().ToString(),
            TimeStamp = DateTime.UtcNow,
            Message = notification.Message,
            SenderName = notification.SenderName
        };

        var recipientNames = await GetRecipientNamesFromNotificationHub();

        var recipientNamesString = CreateCustomRecipientNamesString(recipientNames);

        string notificationJsonPayload =
            "{\"data\" : " +
            "   {" +
            "   \"message\": \"" + notificationToSave.Message + "\"," +
            "   \"senderName\": \"" + notificationToSave.SenderName + "\"," +
            "   \"recipientNames\": \"" + recipientNamesString + "\"" +
            "   }" +
            "}";

        var result = await _hubClient.SendGcmNativeNotificationAsync(notificationJsonPayload, "ken@test.com"); // If this second parameter is omitted then a notification is sent to all registered devices.

        notificationToSave.TrackingId = result.TrackingId;
        notificationToSave.Recipients = recipientNames;

        await Session.StoreAsync(notificationToSave);

        return Ok(notificationToSave);
    }

这就是我在 Android 端注册设备的方式:

private void sendRegistrationIdToBackend(String registrationId) {

        String backendBaseUrl = "http://myurl.net/";
        if (backendBaseUrl == null || backendBaseUrl == "")
        {
            return;
        }

        PushNotificationClient client = new PushNotificationClient(backendBaseUrl);

        Device device = createDevice(registrationId);

        client.registerDevice(device, new Callback<Device>() {
            @Override
            public void success(Device device, Response response) {
                //writeStringToSharedPreferences(SettingsActivity.SETTINGS_KEY_DEVICEGUID, device.DeviceGuid);
                Toast.makeText(context, "Device successfully registered with backend, DeviceGUID=" + device.DeviceGuid, Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(RetrofitError retrofitError) {
                Toast.makeText(context, "Backend registration error:" + retrofitError.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

        Log.i(TAG, registrationId);
    }

    private Device createDevice(String registrationId) {
        Device device = new Device();
        device.Platform = "Android";
        device.Token = registrationId;
        device.UserName = LogInActivity.loggedInUser;
        device.DeviceGuid = null;
        //todo set device.PlatformDescription based on Android version
        device.SubscriptionCategories = new ArrayList<>();
        device.SubscriptionCategories.add("ken@test.com"); // This should be adding this username as a Tag which is referenced in the service.... Not sure if this is how I should do it!
        return device;
    }

这就是我注册设备的方式:

 private async Task<RegistrationDescription> RegisterDeviceWithNotificationHub(Device device)
    {
        var hubTags = new HashSet<string>()
            .Add("user", new[] { device.UserName })
            .Add("category", device.SubscriptionCategories);

        var hubRegistrationId = device.HubRegistrationId ?? "0";//null or empty string as query input throws exception
        var hubRegistration = await _hubClient.GetRegistrationAsync<RegistrationDescription>(hubRegistrationId);
        if (hubRegistration != null)
        {
            hubRegistration.Tags = hubTags;
            await _hubClient.UpdateRegistrationAsync(hubRegistration);
        }
        else
        {
            hubRegistration = await _hubClient.CreateGcmNativeRegistrationAsync(device.Token, hubTags);
        }
        return hubRegistration;
    }

【问题讨论】:

    标签: c# android azure notifications azure-notificationhub


    【解决方案1】:

    晚上好。 您应该尝试使用 Visual Studio 2015 检查您的注册。 安装它,连接 azure、azure hubs、注册设备。并尝试在测试令牌上发送您的测试消息(+检查“您的注册是否存在于通知中心”)。

    【讨论】:

    • 我已经检查了这个并发送了测试通知,但它仍然只适用于广播。它说我注册了 0 个标签。我不知道如何注册标签..?
    • 当您在通知中心注册时,您会传递 2 个参数:1) deviceToken,2) 令牌。之后,在通知中心上创建了具有新 regId 和到期日期的新记录。 msdn.microsoft.com/en-us/library/azure/dn530747.aspx(创建注册)msdn.microsoft.com/ru-ru/library/…(设置标签)
    • 感谢您回复我!如果我想包含标签“ken@test.com”,如何修改这行代码var result = await _hubClient.SendGcmNativeNotificationAsync(notificationJsonPayload);?我尝试在注册设备时将其作为标签插入,并在发布通知时引用它,但这不起作用
    • msdn.microsoft.com/ru-ru/library/… 您应该在注册时添加标签。并在您尝试发送某些通知时使用它们。所以。让我们尝试一些测试颂歌?类似 List tags = new List(); tags.Add("testTag"); hubRegistration = await _hubClient.CreateGcmNativeRegistrationAsync(device.Token, tags); await _hubClient.SendGcmNativeNotificationAsync("{\"data\" : {\"message\": \"testMessage\"} }", "testTag");
    • 谢谢谢尔盖,我现在可以正常工作了!感谢您的帮助
    猜你喜欢
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2017-02-24
    • 2017-08-25
    • 1970-01-01
    • 2014-09-09
    相关资源
    最近更新 更多