Java 将推送通知从ASP.NET发送到Android应用程序
我想从我的ASP.NET WEB API Post方法向我的简单Android应用程序发送一个小推送通知。。这就是我所尝试和研究的 我正在使用谷歌云消息服务向应用程序发送通知。我的应用程序以名称-值对的形式接收它。这里我还提供了服务器代码的Java版本,以及我的Post方法中的C#实现。但是我的方法抛出了一个异常 Android应用程序中出现“异常,错误为(15913):println需要一条消息” 服务器部分的工作Java代码如下所示:-Java 将推送通知从ASP.NET发送到Android应用程序,java,c#,android,asp.net,android-notifications,Java,C#,Android,Asp.net,Android Notifications,我想从我的ASP.NET WEB API Post方法向我的简单Android应用程序发送一个小推送通知。。这就是我所尝试和研究的 我正在使用谷歌云消息服务向应用程序发送通知。我的应用程序以名称-值对的形式接收它。这里我还提供了服务器代码的Java版本,以及我的Post方法中的C#实现。但是我的方法抛出了一个异常 Android应用程序中出现“异常,错误为(15913):println需要一条消息” 服务器部分的工作Java代码如下所示:- public void sendmessage2Dev
public void sendmessage2Device(View v,String regisID,String msg1,String msg2) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"https://android.googleapis.com/gcm/send");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("registration_id",regisID));
nameValuePairs.add(new BasicNameValuePair("data1",msg1));
nameValuePairs.add(new BasicNameValuePair("data2", msg2));
post.setHeader("Authorization","key=AIzaSyBB6igK9sYYBTSIly6SRUHFVexeOa_7FuM");
post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
InputStreamReader inputst = new InputStreamReader(response.getEntity().getContent());
BufferedReader rd = new BufferedReader(inputst);
String line = "";
while ((line = rd.readLine()) != null) {
Log.e("HttpResponse", line);
String s = line.substring(0);
Log.i("GCM response",s);
//Toast.makeText(v.getContext(), s, Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
e.printStackTrace();
}
在Android应用程序中,我有以下接收器位置
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION"))
{
String registrationId = intent.getStringExtra("registration_id");
Log.i("Received the Registration Id as ",registrationId);
String error = intent.getStringExtra("error");
String unregistered = intent.getStringExtra("unregistered");
}
else if (action.equals("com.google.android.c2dm.intent.RECEIVE"))
{
Log.i("In the RECEIVE Method ",intent.getStringExtra("Account"));
String Account = intent.getStringExtra("Account");
String Amount = intent.getStringExtra("Amount");
Log.i("Received Account as ",Account);
Log.i("Received Amount as ",Amount);
}
}
catch(Exception e)
{
Log.i("Exception Occured with Error as ",e.getMessage());
}
finally
{
}
我对Android开发非常陌生,这是我的第一个Helloworld Android应用程序。有人能告诉我我做错了什么,为什么抛出异常以及如何更正它吗?经过一些实质性的研究后发现了这个问题。我假设Log.I()在内部使用println将消息打印到日志中。由于GCM没有推送我的客户端代码正在查找的任何数据,因此它会引发异常。撇开这一点不谈,我得到了推送通知的工作,这里是我学到的,因为它可能对其他有同样问题的人有用
public String Post([FromBody]TransactionDetails value, HttpRequestMessage receivedReq)
{
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
httpWebRequest.ContentType = "application/json; charset=UTF-8";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", "key=AIzaSyBs_eh4nNVaJl3FjQ_ZC72ZZ6uQ2F8r8W4");
String result = "";
String yourresp = "<html><head><title>Response from Server</title></head><body>";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"data\":" +"{\"Amount\":"+value.Amount+","+
"\"Account\":"+value.ToAccountNumber+"}"+","+
"\"registration_ids\":[" + "\""+value.RegID +"\"]}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
DialogResult result1 = MessageBox.Show("Server sent the details to your phone, Check and Confirm to Continue or Not", "Important Information",MessageBoxButtons.YesNo);
if (result1.ToString().Contains("Yes"))
{
WriteAccountNumber(value.ToAccountNumber, value.Amount);
yourresp += "<h1>The Transaction was Successful</h1>";
}
else
{
yourresp += "<h1>The Transaction was NOT Successful</h1>";
}
yourresp += "</body></html>";
return yourresp;
}
catch (Exception e)
{
Console.WriteLine("Exception Occured in Post Method on Web Server as:{0}", e.Message);
return e.Message;
}
}
所以,现在我只想知道如何用OK按钮在警报或任何类型的对话框中显示收到的通知。有谁能帮助我在我的应用程序中使用什么类和如何向用户显示它?如果应用程序未激活,它可以显示在通知栏中,否则需要一个带有“确定”按钮的弹出窗口
谢谢,您是否发布了完整日志?在异常发生之前,您是否收到了“作为#收到的注册Id”?Logcat plz06-04 10:33:24.890:我/收到的注册Id为(19955):APA91bHzO52-3TSEIMV3RXI45ICI9HOESIPL3GRYWAW59ATHVAGPZN86HEHOQJNU8SYJU-YD9UQSQGKZOQLLGI5AOSPMRRUW3RYGHGAPFL0ABLOA2GWFQCZGTOG\U AM0ROG00LBV8OSZ6ZMTP1JBF1S9\U DIWIQRT5WWKW6QFZ4PQYE0JCHSE 06-04 10:34:17.606:我/根据需要打印错误信息时发生异常(19955)
public String Post([FromBody]TransactionDetails value, HttpRequestMessage receivedReq)
{
try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
httpWebRequest.ContentType = "application/json; charset=UTF-8";
httpWebRequest.Method = "POST";
httpWebRequest.Headers.Add("Authorization", "key=AIzaSyBs_eh4nNVaJl3FjQ_ZC72ZZ6uQ2F8r8W4");
String result = "";
String yourresp = "<html><head><title>Response from Server</title></head><body>";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"data\":" +"{\"Amount\":"+value.Amount+","+
"\"Account\":"+value.ToAccountNumber+"}"+","+
"\"registration_ids\":[" + "\""+value.RegID +"\"]}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
}
DialogResult result1 = MessageBox.Show("Server sent the details to your phone, Check and Confirm to Continue or Not", "Important Information",MessageBoxButtons.YesNo);
if (result1.ToString().Contains("Yes"))
{
WriteAccountNumber(value.ToAccountNumber, value.Amount);
yourresp += "<h1>The Transaction was Successful</h1>";
}
else
{
yourresp += "<h1>The Transaction was NOT Successful</h1>";
}
yourresp += "</body></html>";
return yourresp;
}
catch (Exception e)
{
Console.WriteLine("Exception Occured in Post Method on Web Server as:{0}", e.Message);
return e.Message;
}
}
else if (action.equals("com.google.android.c2dm.intent.RECEIVE"))
{
//Log.i("Message Received","Before Scheme");
String Account=intent.getStringExtra("Account");
String Amount=intent.getStringExtra("Amount");
Toast.makeText(context, "Your Transaction Details Received by the Server are...\n\nAccount="+Account+"\nAmount="+Amount, Toast.LENGTH_LONG).show();
Log.i("Account=",Account);
Log.i("Amount=",Amount);
}