IOS platform sends JSON payload in following format:
{"aps":{"alert":"Notification Hub test notification"}}
whereas Android payload format is:
{"data":{"message":"Notification Hub test notification"}}
My SendBroadcastNotification:
public void SendBroadcastNotification(string message) {
NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString(Constants.NotificationsHubConnectionString, "QiKStayNotificationHub",true);
var notify = "{ \"data\" : {\"message\":\"" + message + "\"}}";
var appnotify = "{ \"aps\" : {\"alert\":\"" + message + "\"}}";
var task = hub.SendGcmNativeNotificationAsync(notify);
task.Wait();
}
Since here I am sending notification to android SendGcmNativeNotificationAsync specifically I want to broadcast it to all devices.
So shall I change the payload JSON format likewise
hubClient.SendAppleNativeNotificationAsync();
hubClient.SendGcmNativeNotificationAsync(notify);
While registering device with notification hub you should maintain user's platform
switch (platform.ToLower())
{
case "apns":
notificationMessage = "{ \"aps\" : {\"alert\":\"" + message + "\"}}";
break;
case "gcm":
notificationMessage = "{ \"data\" : {\"message\":\"" + message + "\",\"display\":\"" + title + "\"}}";
break;
default:
break;
}
or you can user template message
private static async void SendTemplateNotificationAsync()
{
// Define the notification hub.
NotificationHubClient hub =
NotificationHubClient.CreateClientFromConnectionString(
"<connection string with full access>", "<hub name>");
// Sending the notification as a template notification. All template registrations that contain
// "messageParam" or "News_<local selected>" and the proper tags will receive the notifications.
// This includes APNS, GCM, WNS, and MPNS template registrations.
Dictionary<string, string> templateParams = new Dictionary<string, string>();
// Create an array of breaking news categories.
var categories = new string[] { "World", "Politics", "Business", "Technology", "Science", "Sports"};
var locales = new string[] { "English", "French", "Mandarin" };
foreach (var category in categories)
{
templateParams["messageParam"] = "Breaking " + category + " News!";
// Sending localized News for each tag too...
foreach( var locale in locales)
{
string key = "News_" + locale;
// Your real localized news content would go here.
templateParams[key] = "Breaking " + category + " News in " + locale + "!";
}
await hub.SendTemplateNotificationAsync(templateParams, category);
}
}
Related
I have a notification hub, and it, have a hub-namespace and it is almost full.
I'll create another hub, how do I programmatically know (Android), which one should I enter the user?
Android source code:
public class NotificationSettings {
public static String SenderId = "MyFirebaseSenderId";
public static String HubName = "myhub-hub";
public static String HubListenConnectionString = "Endpoint=sb://myapp.serv.../;SharedAccessKeyName=D..ure;SharedAccessKey=K..t/n8I/X..=";
}
RegistrationIntent:
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
private NotificationHub hub;
public RegistrationIntentService() {
super(TAG);
}
public ApplicationUtils getApplicationUtils() {
return (ApplicationUtils) getApplication();
}
#Override
protected void onHandleIntent(Intent intent) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String resultString = null;
String regID = null;
String storedToken = null;
String tag = null;
try {
tag ="_UserId:" + getApplicationUtils().getUsuario().Id;
} catch (Exception e){
return;
}
try {
if(FirebaseInstanceId.getInstance() == null){
FirebaseApp.initializeApp(this);
}
String FCM_token = FirebaseInstanceId.getInstance().getToken();
SaveSharedPreferences.setFCM(getApplicationContext(),FCM_token);
Log.d(TAG, "FCM Registration Token: " + FCM_token);
// Storing the registration id that indicates whether the generated token has been
// sent to your server. If it is not stored, send the token to your server,
// otherwise your server should have already received the token.
if (((regID=sharedPreferences.getString("registrationID", null)) == null)){
NotificationHub hub = new NotificationHub(NotificationSettings.HubName,
NotificationSettings.HubListenConnectionString, this);
Log.d(TAG, "Attempting a new registration with NH using FCM token : " + FCM_token);
regID = hub.register(FCM_token, tag).getRegistrationId();
// If you want to use tags...
// Refer to : https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-routing-tag-expressions/
// regID = hub.register(token, "tag1,tag2").getRegistrationId();
resultString = "New NH Registration Successfully - RegId : " + regID;
Log.d(TAG, resultString);
sharedPreferences.edit().putString("registrationID", regID ).apply();
sharedPreferences.edit().putString("FCMtoken", FCM_token ).apply();
}
// Check if the token may have been compromised and needs refreshing.
else if (!((storedToken=sharedPreferences.getString("FCMtoken", "")).equals(FCM_token))) {
NotificationHub hub = new NotificationHub(NotificationSettings.HubName,
NotificationSettings.HubListenConnectionString, this);
Log.d(TAG, "NH Registration refreshing with token : " + FCM_token);
regID = hub.register(FCM_token, tag).getRegistrationId();
// If you want to use tags...
// Refer to : https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-routing-tag-expressions/
// regID = hub.register(token, "tag1,tag2").getRegistrationId();
resultString = "New NH Registration Successfully - RegId : " + regID;
Log.d(TAG, resultString);
sharedPreferences.edit().putString("registrationID", regID ).apply();
sharedPreferences.edit().putString("FCMtoken", FCM_token ).apply();
}
else {
resultString = "Previously Registered Successfully - RegId : " + regID;
}
} .............................................
Now, it follows my downend code below, I do not know if it would be important in this case. But, it is developed in C # .Net:
public static async void sendPushNotification(ApiController controller, DataObjects.Notification notification)
{
// Get the settings for the server project.
HttpConfiguration config = controller.Configuration;
MobileAppSettingsDictionary settings =
controller.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
// Get the Notification Hubs credentials for the Mobile App.
string notificationHubName = settings.NotificationHubName;
string notificationHubConnection = settings
.Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString;
// Create a new Notification Hub client.
NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
// Android payload
JObject data = new JObject();
data.Add("Id", notification.Id);
data.Add("Descricao", notification.Descricao);
...
//alteração com a colocação da tag priority em caso de erro teste sem
var androidNotificationPayload = "{ \"data\" : {\"message\":" + JsonConvert.SerializeObject(data) + "}}";
try
{
// Send the push notification and log the results.
String tag = "_UserId:"+notification.Id_usuario;
//var result = await hub.SendGcmNativeNotificationAsync(androidNotificationPayload);
var result = await hub.SendGcmNativeNotificationAsync(androidNotificationPayload, tag);
// Write the success result to the logs.
config.Services.GetTraceWriter().Info(result.State.ToString());
}
catch (System.Exception ex)
{
// Write the failure result to the logs.
config.Services.GetTraceWriter().Error(ex.Message, null, "Push.SendAsync Error");
}
}
I need a lot of help on this issue.
Thank you so much for this.
According to your description, I suggest you could write a api method which will insert new user to the notification hub(not on the client side) after checking the registrations is full or not.
We could use NotificationHubClient.GetAllRegistrationsAsync Method to get all AllRegistrations to local. Then we could count its number.
After checking successfully, then we will check enter user to which hub.
The workflow is:
New user registered:
Client: Send the user information to server web api method
Server: Check the notification hub is full(By calling NotificationHubClient.GetAllRegistrationsAsync or directly registered user if it failed registered into new hub)
Besides, notification hub provides different pricing tier.
I suggest you could consider scaling up your pricing tier to support multiple active devices.
I have this line which needs to recieve a message from the discord text channel.
choice = video[int.Parse(CommandHandler.message .Content)-1].Url;
I tried a lot of things, including searching in the api but I don't have a clue.
Here is the command
[Command("join", RunMode= RunMode.Async), Summary("joins voice channel")]
public async Task joinvoice([Remainder, Summary("The text to echo")] string searchP="")
{
IVoiceChannel voicechannel = (CommandHandler.Last as IGuildUser).VoiceChannel;
if (voicechannel == null)
{
await ReplyAsync("u have to be in a channel first");
return;
}
string choice = "";
VideoSearch SearchRisolts = new VideoSearch();
if (searchP != "")
{
if (searchP.Contains("https://"))
choice = searchP;
else
{
List<VideoInformation> video = SearchRisolts.SearchQuery(searchP, 1);
await ReplyAsync("1) " + video[0].Title + "\n\n2) " + video[1].Title + "\n\n3) " + video[2].Title);
choice = video[int.Parse(CommandHandler.message .Content)-1].Url;//here i need to recive a message from the chat
}
}
if (voicechannel != Program.voicechannel)
{
Program.audioClient = await voicechannel.ConnectAsync();
Program.voicechannel = voicechannel;
}
if (Program.audioClient!=null)
await SendAsync(Program.audioClient, choice);
}
The class containing this function should derive from BaseModule<ICommandContext>
This BaseModule contains the Context, which in place contains the message you are looking for.
Context.Message.Content
I am currently building an Android messaging app and I am trying to send notifications to all users who are part of a group. I have an Azure Notification Hub set up which is working fine for sending a notification to all devices that are registered, but I cannot seem to get it working for just a subset of all user ie a group.
The devices register with Azure and with GCM on startup.
I have tried using "Tags" to try send a notification to an individual but I am not sure if I am doing it right... I must not be because it isn't working!
In the following code I am trying to send a notification to an individual by using their username as a Tag...
This is the code in my service to send a notification:
// 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);
}
And this is how I am registering a device on the Android side:
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;
}
This is how I registering a 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;
}
good evening.
You should try check your registrations by using Visual studio 2015.
Install it, connect azure, azure hubs, registred devices. And try send your test message on test token (+check "does your registration exist on notification hub").
How to send parameter in REST api in xamarin.forms ?
I have created REST API Project in xamarin using PCL.
When I call Simple REST api using Below code in Xamarin.forms (Portable Class Library) then I have Successfully receive json Response.
using (var cl = new HttpClient())
{
var result = await cl.GetStringAsync("http://192.168.1.100/apps/jara/web/api/user/test");
jsonResponseClass des = JsonConvert.DeserializeObject<jsonResponseClass>(result);
lbl1.Text = des.code + " " + des.status + " " + des.message;
}
public class jsonResponseClass
{
public string code { get; set; }
public string status { get; set; }
public string message { get; set; }
}
Using above code I got a response
{
code: 200,
status: "ok",
message: "hello"
}
REST API response type is JSON and type is POST
Now, I want to call below Login Api using paramater.
http://192.168.1.100/apps/jara/web/api/user/login
Paramater : email_id and Password
this api success response type is....
{
code: 200,
status: "Success",
User: {
userid: 126,
token: "d154s4d54s654df5s4df56s4df564s5df4",
email: "shahbuddin#gmail.com",
mobile_number: "9898989898"
},
message: "Successfully logged in"
}
What can i do ?
Finally i can do this using below code....
using (var cl = new HttpClient())
{
var formcontent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string,string>("email_id","shahbuddin#peerbits.com"),
new KeyValuePair<string, string>("password","shah")
});
var request = await cl.PostAsync("http://192.168.1.100/apps/jara/web/api/user/login", formcontent);
request.EnsureSuccessStatusCode();
var response = await request.Content.ReadAsStringAsync();
jsonResponselogin res = JsonConvert.DeserializeObject<jsonResponselogin>(response);
lbl1.Text = res.code + " " + res.status + " " + res.message;
}
This code helpful for Calling REST API with Parameter
Thank you...
Outline
I am trying to implement WNS for my app game. I currently send a notification upon creation of a new user with the following function which works:
public async void SendNotificationToTag(string tag, string content)
{
var wnsToast = "<toast><visual><binding template=\"ToastText01\">"
+ "<text id=\"1\">Breaking " +content + "An WNS News!"
+ "</text></binding></visual></toast>";
WindowsPushMessage wnsMessage = new WindowsPushMessage();
wnsMessage.XmlPayload = wnsToast;
await Services.Push.HubClient.SendWindowsNativeNotificationAsync(wnsToast, tag);
Services.Log.Info("WNS TEST - SendWindowsNativeNotificationAsync - done");
}
I get a notfication to each username, i.e. a personal notificaion. I then update the tags the user listens to, looking in the hub database this also seems to work:
-Usernamecph--gameID1151--gameID1152--gameID1153--gameID1154--gameID1155--gameID1156--gameID1157--gameID1158
-gameID1157--UsernameFyn--gameID1151--gameID1153--gameID1155--gameID1156-
This check to extract the tags from the hub is done using
foreach (Microsoft.ServiceBus.Notifications.RegistrationDescription t in a)
{
string tempstring = "";
foreach (string x in t.Tags)
tempstring += "-" + x + "-";
Services.Log.Info(tempstring + t.RegistrationId + t.ETag);
}
So far so good.
When I then try to send to one of the other tags than the usernames I do not receive any notification, and I do not receive any error in the log. Am I missing something?
Update - Half Solution
If I use the server Explorer and look at the notification HUB. I can see all the tags, and I can send to them by using the test send. But I cannot seem to do it in other function calls online.
Is it something like the function has to be set as post or get?
YES
So inserting [HttpPost] Seems to enable the push.
However when I look in the Server explorer shown here on the image:
The user seems to be deleted when I update the registrations (There should be three, and there is again when I start the app with the correct tag subscriptions). So Maybe the question is How to Update a registration in the hub correctly
The current update code:
public async void updateRegistration(RegistrationDescription registration, List<string> TAGS)
{
registration.Tags = new HashSet<string>(TAGS);
try
{
var x = await hub.CreateOrUpdateRegistrationAsync(registration);
// apiServices.Log.Info(x.ExpirationTime + " " + x.Tags);
}
catch (MessagingException e)
{
ReturnGoneIfHubResponseIsGone(e);
}
}
The code that calls the function:
private async Task<bool> RegisterHubTag(User user, string Tag)
{
List<string> sendTAGs = new List<string>();
Services.Log.Info("RegisterHubTag Function");
using (Db db = new Db())
{
List<DataObjects.NotificationTag> userTags = db.NotificationTags.Where(t => t.User.UserId == user.UserId).ToList<DataObjects.NotificationTag>();
if (userTags.Count < 1)
{
//Register
RegisterController.DeviceRegistration Reg = CreateDeviceRegistration(user.PushChannelUri, sendTAGs);
Microsoft.Azure.NotificationHubs.RegistrationDescription registration = null;
//Microsoft.ServiceBus.Notifications.RegistrationDescription registration = null;
Services.Log.Info(Reg.Handle);
Services.Log.Info(Reg.Platform);
IEnumerable<string> tagsToRegister;
List<string> test = new List<string>() { user.Username };
if(Tag != user.Username)
test.Add(Tag);
tagsToRegister = test.AsEnumerable<string>();
switch (Reg.Platform)
{
case "mpns":
registration = new MpnsRegistrationDescription(Reg.Handle);
break;
case "wns":
registration = new WindowsRegistrationDescription(Reg.Handle);
break;
case "apns":
registration = new AppleRegistrationDescription(Reg.Handle);
break;
case "gcm":
registration = new GcmRegistrationDescription(Reg.Handle);
break;
default:
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
var regID = await hub.Post(Services);
registration.RegistrationId = regID;
db.NotificationTags.Add(new DataObjects.NotificationTag() { User = user, tag = user.Username, RegistrationID = registration.RegistrationId });
hub.updateRegistration(registration, test);
db.SaveChanges();
}
else
{
RegisterController.DeviceRegistration Reg = CreateDeviceRegistration(user.PushChannelUri, sendTAGs);
Microsoft.Azure.NotificationHubs.RegistrationDescription registration = null;
//Microsoft.ServiceBus.Notifications.RegistrationDescription registration = null;
switch (Reg.Platform)
{
case "mpns":
registration = new MpnsRegistrationDescription(Reg.Handle);
break;
case "wns":
registration = new WindowsRegistrationDescription(Reg.Handle);
break;
case "apns":
registration = new AppleRegistrationDescription(Reg.Handle);
break;
case "gcm":
registration = new GcmRegistrationDescription(Reg.Handle);
break;
default:
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
registration.RegistrationId = userTags[0].RegistrationID;
IEnumerable<string> tagsToRegister;
List<string> test = new List<string>();
foreach (DataObjects.NotificationTag t in userTags)
test.Add(t.tag);
test.Add(Tag);
tagsToRegister = test.AsEnumerable<string>();
hub.updateRegistration(registration, test);
}
}
return true;
}
private RegisterController.DeviceRegistration CreateDeviceRegistration(string channelUri, List<string> tags)
{
return new RegisterController.DeviceRegistration() { Platform = "wns", Handle = channelUri, Tags = tags.ToArray<string>() };
}
New image
I really do not understand it, sometimes I have three registrations, but then in the counter in the bottom is still only saying 2 ? How can this be?
(The view is from the server explorer in VS2013)