C# 使用azure向IOS和android发送广播通知
IOS平台以以下格式发送JSON负载:C# 使用azure向IOS和android发送广播通知,c#,azure,notifications,C#,Azure,Notifications,IOS平台以以下格式发送JSON负载: {"aps":{"alert":"Notification Hub test notification"}} 而Android有效负载格式为: {"data":{"message":"Notification Hub test notification"}} 我的发送广播通知: public void SendBroadcastNotification(string message) { NotificationHubClient hub =
{"aps":{"alert":"Notification Hub test notification"}}
而Android有效负载格式为:
{"data":{"message":"Notification Hub test notification"}}
我的发送广播通知
:
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();
}
由于我在这里向android SendGcmNativeNotificationAsync发送通知,因此我特别希望将其广播到所有设备 那么,我应该同样地更改有效负载JSON格式吗
hubClient.SendAppleNativeNotificationAsync();
hubClient.SendGcmNativeNotificationAsync(notify);
在向通知中心注册设备时,您应该维护用户的平台
switch (platform.ToLower())
{
case "apns":
notificationMessage = "{ \"aps\" : {\"alert\":\"" + message + "\"}}";
break;
case "gcm":
notificationMessage = "{ \"data\" : {\"message\":\"" + message + "\",\"display\":\"" + title + "\"}}";
break;
default:
break;
}
或者您可以使用用户模板消息
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);
}
}
private static async void SendTemplateNotificationAsync()
{
//定义通知中心。
NotificationHub客户端集线器=
NotificationHubClient.CreateClientFromConnectionString(
"", "");
//将通知作为模板通知发送。包含
//“messageParam”或“News_”以及适当的标记将接收通知。
//这包括APNS、GCM、WNS和MPNS模板注册。
Dictionary templateParams=新字典();
//创建突发新闻类别数组。
var categories=新字符串[]{“世界”、“政治”、“商业”、“技术”、“科学”、“体育”};
var locales=新字符串[]{“英语”、“法语”、“普通话”};
foreach(类别中的var类别)
{
templateParams[“messageParam”]=“Breaking”+类别+“News!”;
//正在为每个标签发送本地化新闻。。。
foreach(区域设置中的变量区域设置)
{
string key=“News\”+区域设置;
//您真正的本地化新闻内容将放在这里。
templateParams[key]=“Breaking”+类别+“新闻在”+区域设置+“!”;
}
wait hub.SendTemplateNotificationAsync(templateParams,category);
}
}
Hi Mahesh,我想广播通知,那么为什么我需要检查pns标记?在json有效负载中是否有其他方法可以放置更多标记,同样是“{\'data\”:{\'message\”:\\'+message+“\'}}”,或者我有动态参数可以发送var dynmicjson=“{'data':{'message':'$(message)”,'icon':$(icon){$(params)}}}}“您好,谢谢!我几乎采用了相同的方法var users=(从数据库中的u开始。用户选择新的{u.Name}”);foreach(用户中的var user){var notify=“{data\”:{message\:\”+“message+”,\”userId\“:\”+user.Name+“\”}”;var-alert=“{\”aps\“:{\”alert\“:\”+message+“\”}”;var task=hub.SendGcmNativeNotificationAsync(notify);SendGcmNativeNotificationAsync将广播到所有android设备,而SendAppleNativeNotificationAsync将广播到所有apple设备