如何在不使用Firebase控制台的情况下将Firebase云消息传递通知发送到iOS设备?

时间:2016-08-03 08:49:20

标签: ios firebase apple-push-notifications firebase-cloud-messaging

如果我从FCM控制台向iOS设备发送通知,我会在通知中心收到消息提示(从屏幕顶部向下滑动以查看通知中心)。

didRecievedRemoteNotification输出为:

{

    aps =     {

        alert = "From console";

    };

    "gcm.message_id" = "0:1470206236110595%b2c76869b2c76869";

    "gcm.n.e" = 1;

    "google.c.a.c_id" = 2979094970349938289;

    "google.c.a.e" = 1;

    "google.c.a.ts" = 1470206236;

    "google.c.a.udt" = 0;

}

但是如果向iOS设备发送通知我正在使用服务器上的api发送我没有在通知中心收到消息提醒(从屏幕顶部向下滑动以查看通知中心)。< / p>

didRecievedRemoteNotification输出是:

{

    "collapse_key" = "do_not_collapse";

    from = 67981113117;

    message = "alert from api";

    time = "03-08-2016 12:44:53";

}

我的API代码在C#中:

string RegIDs = "some id";





                var appID = "some id";


                var SenderID = "some id";
                var value = Text1.Text;
                WebRequest tRequest;



            tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");

                tRequest.Method = "POST";
                tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
                tRequest.Headers.Add(string.Format("Authorization: key={0}", appID));

                tRequest.Headers.Add(string.Format("Sender: id={0}", SenderID));


                //Data_Post Format
                // string postData = "{'collapse_key' : 'demo', 'registration_id': [ '" + regId + "' ], 
                //'data': {'message': '" + Label1.Text + "'},'time_to_live' : '3' }";

            //json for android 
                string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message="
                   + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + RegIDs + "";



            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 = (WebResponse)tRequest.GetResponse();


                dataStream = tResponse.GetResponseStream();

                StreamReader tReader = new StreamReader(dataStream);

                String sResponseFromServer = tReader.ReadToEnd();

                Label3.Text = sResponseFromServer; //printing response from GCM server.
                tReader.Close();
                dataStream.Close();
                tResponse.Close();

0 个答案:

没有答案