public async Task SendPushNotificationAsync(PushNotification pushNotification, params string[] tags)
{
if (string.IsNullOrWhiteSpace(pushNotification?.Title)) return;
try
{
var settings = new JsonSerializerSettings { ContractResolver = new LowerCaseContractResolver() };
var platforms = pushNotification.Platforms.GetFlags<PushPlatform>();
foreach (var platform in platforms)
{
switch (platform)
{
case PushPlatform.APNS:
var apnsAlert = $"{{ \"aps\" : {{ \"alert\" : {{ \"title\" : \"{pushNotification.Title}\", \"body\" : \"{pushNotification?.Body}\" }}}}}}";
await notificationHub.SendAppleNativeNotificationAsync(apnsAlert, tags);
break;
case PushPlatform.GCM:
var simplePushNotificationMessage = new
{
Title = pushNotification.Title,
Body = pushNotification.Body
};
var gcmAlert = $"{{ \"data\" : {JsonConvert.SerializeObject(simplePushNotificationMessage, settings)} }}";
await notificationHub.SendGcmNativeNotificationAsync(gcmAlert, tags);
break;
case PushPlatform.WNS:
var toast = $"<toast launch=\"{pushNotification?.PushNotificationLaunchArgument}\"><visual><binding template=\"ToastText02\"><text id=\"1\">{pushNotification.Title }</text><text id=\"2\">{ pushNotification.Body }</text></binding></visual></toast>";
await notificationHub.SendWindowsNativeNotificationAsync(toast, tags);
break;
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}