PushSharp - Android GCM推送通知 [英] PushSharp - Android GCM push notification
问题描述
我在这里解释了我的问题..
http://stackoverflow.com/questions/13268460/pushsharpandroid-gcm-push-notification-received-without-push-message [ ^ ]
我正在使用PushSharp库从我的应用程序发送推送通知。
I have explained my question here..
http://stackoverflow.com/questions/13268460/pushsharpandroid-gcm-push-notification-received-without-push-message[^]
I am using PushSharp library to send push notification from my application.
PushService push = new PushService();
var reg_id_d = "APA91bETd-LsqnZjA-HKrnBOY3FbEhmWchpiwuhRkiv4gUdGDuvwDRB7YURICZ131XppDAUNUBLGe_vEPkQ-JR8UaVX7Y-NCkEfastCBLIYcUoFtt5cPafeKXHywi0WGDYW33ZQqr3oy";
var project_id_d = "482885626272";
var api_key_d = "AIzaSyAbh7R5KQR3KM7W_y-yS-Ao-JNiihNz7tE"; // "AIzaSyDcKfuW77GTwA46L6sqD41YhGf2j5S8o2w";
var package_name_d = "com.get.deviceid";
push.StartGoogleCloudMessagingPushService(new GcmPushChannelSettings(project_id_d, api_key_d, package_name_d));
push.QueueNotification(NotificationFactory.AndroidGcm()
.ForDeviceRegistrationId(reg_id_d)
.WithCollapseKey("NONE")
.WithJson("{\"alert\":\"Alert Text!\",\"badge\":\"1\"}"));
我的设备收到通知但是留言空白..
我尝试用C#中的服务器代码发送GCM推送通知,但同样有空白消息的问题。
我尝试使用PHP发送通知。它按预期工作。所以,我不确定上面的代码有什么问题。任何人都可以帮助我吗?
I am getting notification on my device but with blank message..
I have tried with sever code available in C# to send GCM push notification, but getting same problem of having blank message.
I tried using PHP to send notification. and it is working as expected. so, I am not sure what is wrong in my above code. Can anyone please help me on this?
推荐答案
在本文中,我将尝试解释如何使用ASP.NET和C#集成Android推送通知服务。众所周知,移动应用正在蓬勃发展的市场趋势。某些自定义移动应用程序使用推送通知服务为应用程序用户提供更新。在这里,我将解释如何使用Google的GCM推送通知服务。
类文件AndroidGCMPushNotification.cs
In this article I will try to explain how we can integrate a push notification service for Android using ASP.NET and C#. We all know that mobile applications are booming the market trend. Some custom mobile applications use the push notification service to give updates to application users. Here I will explain how we can use Google’s GCM push notification service.
Class file "AndroidGCMPushNotification.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;
/// <summary>
/// Summary description for AndroidGCMPushNotification
/// </summary>
public class AndroidGCMPushNotification
{
public AndroidGCMPushNotification()
{
//
// TODO: Add constructor logic here
//
}
public string SendNotification(string deviceId, string message)
{
string GoogleAppID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var SENDER_ID = "9999999999";
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 = "{ ''registration_id'': [ ''" + regId + "'' ], ''data'': {''message'': ''" + txtMsg.Text + "''}}";
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;
}
}
您可以通过传递设备ID来调用SendNotification函数和消息。
You can call SendNotification function by passing device Id and Message.
AndroidGCMPushNotification apnGCM = new AndroidGCMPushNotification();
string strResponse = apnGCM.SendNotification("9999xxxxxxxxxxxxxxxx", "Test Push Notification message ");
check''alert''关键字完全相同在Android应用程序(编码)中...当此关键字匹配时,您的消息通过推送通知发送...
check ''alert'' keyword exact same in android app (in coding) also...when this keyword matches then your message send via push notification...
这篇关于PushSharp - Android GCM推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!