从服务器推送通知

时间:2017-02-26 09:59:58

标签: c# android firebase firebase-cloud-messaging

为了从控制台应用程序向Android设备发送通知,我使用以下代码:

   class Program
{
    static void Main(string[] args)
    {
        string applicationId = "applicationId";
        string senderId = "senderId";
        string deviceId = "deviceId";
        string message = "TestMessage";

        var x = new AndroidFCMPushNotificationStatus();
        var response = x.SendNotification(applicationId, senderId, deviceId, message);
        Console.WriteLine(response);

        Console.ReadLine();
    }
}

public class AndroidFCMPushNotificationStatus
    {
        public string SendNotification(string applicationID, string senderId, string deviceId, string message)
        {
            try
            {
                WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                tRequest.Method = "post";
                tRequest.ContentType = "application/json";
                var data = new
                {
                    to = deviceId,
                    notification = new
                    {
                        body = message,
                        title = message,

                    },
                    priority = "high"
                };

                var serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(data);
                Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationID));
                tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
                tRequest.ContentLength = byteArray.Length;

                using (Stream dataStream = tRequest.GetRequestStream())
                {
                    dataStream.Write(byteArray, 0, byteArray.Length);
                    using (WebResponse tResponse = tRequest.GetResponse())
                    {
                        using (Stream dataStreamResponse = tResponse.GetResponseStream())
                        using (StreamReader tReader = new StreamReader(dataStreamResponse))
                        {
                            string sResponseFromServer = tReader.ReadToEnd();
                            return (sResponseFromServer);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return (ex.Message);
            }
        }
    }

来自Push Notification C#

我创建了一个firebase控制台项目,从项目设置中,从Cloud Messaging选项卡中我获得了Server Key和Sender Id(在代码中它们是applicationId和senderId)。我还从我下载到手机的应用程序中获取了设备ID,它在Android设备ID标签下给了我一个字符串。

但是我收到消息错误:InvalidRegistration。

我对此很陌生,我想我错过了一些东西。

  • 以上代码是否可以正常运行,每当我运行程序时,我都会在手机上收到通知?
  • 我应该做些什么来通知我的手机吗?我的设备上没有配置任何内容。
  • 我的想法是将此代码打包到服务中,并且每当服务器上发生某些事情时我都想使用此代码来通知 我的设备。这是正确的方法吗?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

error:InvalidRegistration表示deviceId(也称为注册令牌)错误。

  

检查传递给服务器的注册令牌的格式。   确保它与客户端应用程序收到的注册令牌相匹配   从注册Firebase通知。不要截断或添加   其他角色。

https://firebase.google.com/docs/cloud-messaging/http-server-ref#error-codes

检查您是否正在阅读正确的令牌:

FirebaseInstanceId.getInstance().getToken()

https://firebase.google.com/docs/cloud-messaging/android/first-message#retrieve-the-current-registration-token