Monday 22 June 2020

Android Firebase Push Notification in asp.net C#



using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;

namespace AnroidPushNotification
{
public class AndriodPushNotification
{

public string SendPushNotificationForAndroidSWithoutImage()
{
PushNotificationParameters parameters = new PushNotificationParameters();

parameters.DeviceId = "Token ID";
parameters.ServerKey = "ServerKey ";
parameters.SenderId = "SenderId";
parameters.RequestTimeOut = 20000;
parameters.Title = "TestTitle";
parameters.SoundEnable = "Enabled";
parameters.NotificationEN = "TestEn";
parameters.NotificationAR = "TestAR";
parameters.Message ="testMessage";
string response;
try
{
WebRequest request = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
request.Timeout = parameters.RequestTimeOut;
request.Method = "Post";
request.ContentType = "application/json";
var data = new
{
//ApplicationId = parameters.ApplicationId,
to = parameters.DeviceId,
priority = "high",
content_available = true,
notification = new
{
title = parameters.Title,
sound = parameters.SoundEnable,
messageAR = parameters.NotificationAR,
messageEN = parameters.NotificationEN
}
};
var json = SerializeAndroidWithoutImage(parameters);
//tracingService.Trace("SendPushNotificationForAndroidWithoutImage: json" + json);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
request.Headers.Add(string.Format("Authorization:key={0}", parameters.ServerKey));
request.Headers.Add(string.Format("Sender: id={0}", parameters.SenderId));
request.ContentLength = byteArray.Length;
// tracingService.Trace("SendPushNotificationForAndroidWithoutImage : ContentLength" + byteArray.Length);

using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
// tracingService.Trace("SendPushNotificationForAndroidWithoutImage:dataStream " + dataStream.Length);

using (WebResponse webResponse = request.GetResponse())
{
using (Stream dataStreamResponse = webResponse.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(dataStreamResponse))
{
string sResponseFromServer = streamReader.ReadToEnd();
response = sResponseFromServer;
}
}
}


}


}
catch (Exception ex)
{
response = ex.Message;
}
return response;
}
public string SerializeAndroidWithoutImage(PushNotificationParameters parameters)
{
string result = string.Empty;
result = "{\"to\":\"" + parameters.DeviceId
+ "\",\"data\":{\"title\":\"" + parameters.Title
+ "\",\"body\":\"" + parameters.Message
+ "\",\"sound\":\"" + parameters.SoundEnable
+ "\",\"messageAR\":\"" + parameters.NotificationAR
+ "\",\"messageEN\":\"" + parameters.NotificationEN
+ "\"}}";


return result;

}
}
}


How to get logged in User's Security Roles using Java Script in dynamic CRM 365.

 function GetloggedUser () {     var roles = Xrm.Utility.getGlobalContext().userSettings.roles;      if (roles === null) return false;      ...