推送通知错误-缺少注册

本文关键字:注册 错误 通知 | 更新日期: 2023-09-27 18:00:57

我已经通过GCM使用ASP.net C#向android手机发送了推送通知。

但我尝试了不同类型的代码,但所有的arr都返回注册丢失错误,所以请帮助我。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PushNotification;
namespace TestAndroidPush
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Main();
            }
        }

        private void Main()
        {
            AndroidGCMPushNotification apnGCM = new AndroidGCMPushNotification();
            string strResponse = apnGCM.SendNotification(device id, "Test Push");
        }
    }
}

cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Collections.Specialized;
public class AndroidGCMPushNotification
{
    public AndroidGCMPushNotification()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public string SendNotification(string deviceId, string message)
    {
        string GoogleAppID = "GoogleAppID";
        var SENDER_ID = "sender id";
        var value = message;
        WebRequest tRequest;
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));
        tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
        string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "®istration_id=" + deviceId + "";
        Console.WriteLine(postData);
        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        tRequest.ContentLength = byteArray.Length;
        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse tResponse = tRequest.GetResponse();
        dataStream = tResponse.GetResponseStream();
        StreamReader tReader = new StreamReader(dataStream);
        String sResponseFromServer = tReader.ReadToEnd();
        tReader.Close();
        dataStream.Close();
        tResponse.Close();
        return sResponseFromServer;
    }
}

推送通知错误-缺少注册

MissingRegistration在您的请求中未包含注册ID时由GCM返回。

对于您正在使用的纯文本请求(基于您的application/x-www-form-urlencoded;charset=UTF-8内容类型(,注册ID应作为&registration_id=your_registration_id而不是您发布的代码中的®istration_id=传递。