0
我目前正在構建一個Android消息傳遞應用程序,並且正在嘗試向屬於某個組的所有用戶發送通知。我設置了一個Azure通知中心,它可以正常發送通知給所有已註冊的設備,但我似乎無法使其僅適用於所有用戶的子集,即組。使用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, "[email protected]"); // 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("[email protected]"); // 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;
}
我已經檢查了這個,併發送了測試通知,但它仍然只能用於廣播..它說我有0個標籤註冊,但是。我不知道如何註冊標籤..? – semiColon
當您在通知中心註冊時,您傳遞2個參數:1)deviceToken,2)令牌。 在新的記錄和過期日期之後在通知中心上創建新記錄之後。 https://msdn.microsoft.com/en-us/library/azure/dn530747.aspx(創建註冊) https://msdn.microsoft.com/ru-ru/library/microsoft.servicebus.notifications .windowsregistrationdescription.aspx(設置標籤) –
謝謝你回到我身邊!如何修改這一行代碼'var result = await _hubClient.SendGcmNativeNotificationAsync(notificationJsonPayload);'如果我想包含標籤「[email protected]」?我試圖在註冊設備時引用它作爲標記並在發佈通知時引用它,但這不起作用 – semiColon