Senden einer Push-Benachrichtigung von einem ASP.NET Android App

Ich will Senden ein kleines Push-Benachrichtigung von meinem ASP.NET WEB-API-Post-Methode, um meine Einfache Android-App.. Hier ist, was ich versucht und neu gesucht.

Ich bin mit der Google-Cloud-Messaging-Dienst zum Senden der Benachrichtigung an die App. Meine App erhält es in Name-Wert-Paare. Ich gebe auch hier die eine funktionierende Java-Version der Server-Code und mein C# - Implementierung in meinem Post-Methode. Aber meine Methode wirft eine Ausnahme, wie
"Ausnahme Aufgetreten mit Fehler(15913): println needs a message" in der Android-App.

Funktionierenden Java-Code wird wie folgt für den Server-Teil:-

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();
    }

Ähnlich wie Mein C# - Code in der ASP.NET WEB API ist wie folgt...

public String Post([FromBody]TransactionDetails value)
    {
        try
        {
            WebClient toGCM = new WebClient();
            toGCM.Headers.Add("ContentType:");
            toGCM.Headers["ContentType"] = "application/x-www-form-urlencoded; charset=UTF-8";
            //HttpWebRequest toGCM = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send");
            //toGCM.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            toGCM.Headers.Add("Accept:");
            toGCM.Headers["Accept"]= "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";
            //toGCM.Accept = "application/json, text/javascript, */*; q=0.01";
            toGCM.Headers.Add("UserAgent:") ;
            toGCM.Headers["UserAgent"]= "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36";
            toGCM.Headers.Add("Authorization:");
            toGCM.Headers["Authorization"] = "key=AIzaSyCoxjeOGi_1ss2TeShDcoYbNcPU9_0J_TY";
            //String DatatoClient = "ToAccountNumber=" + value.ToAccountNumber + "&Amount=" + value.Amount;
            NameValueCollection postFieldNameValue = new NameValueCollection();
            postFieldNameValue.Add("registration_id", "APA91bHzO52-3TsEIMV3RXi45ICIo9HOe1ospPl3gRYwAAw59ATHvadGPZN86heHOQJNU8syju-yD9UQqSgkZOqllGi5AoSPMRRuw3RYhgApflr0abLoAh2GWFQCZgToG_aM0rOg0lBV8osz6ZMtP1JBF1S9_DiWiqKRT5WL6qFz4PqyE0jCHsE");
            postFieldNameValue.Add("Account", value.ToAccountNumber.ToString());
            postFieldNameValue.Add("Amount", value.Amount.ToString());

            byte[] responseArray = toGCM.UploadValues("https://android.googleapis.com/gcm/send", postFieldNameValue);
            String S=Encoding.ASCII.GetString(responseArray);
            return S;
        }
        catch (Exception e)
        {
            Console.WriteLine("Exception Occured in Post Method on Web Server as:{0}", e.Message);
            return e.Message;
        }
    }

In der Android-APP habe ich folgende LOC für Receiver...

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
    {

    }

Ich bin sehr neu in Android Development und ths ist mein erstes Helloworld-App von Android. Kann mir jemand sagen was ich falsch mache und warum die Exception geworfen wird, und wie man es richtig?

  • tun Sie Ihre volle logcat
  • Bekommen Sie 'Erhalten Sie die Registrierungs-Id #' vor die Ausnahme aufgetreten ist ? Logcat plz
  • 06-04 10:33:24.890: ich/Empfangen der Registrierungs-Id(19955): APA91bHzO52-3TsEIMV3RXi45ICIo9HOe1ospPl3gRYwaaw59athvadgpzn86hehoqjnu8syju-yD9UQqSgkZOqllGi5AoSPMRRuw3RYhgApflr0abloah2gwfqczgtog_am0rog0lbv8osz6zmtp1jbf1s9_diwiqkrt5wl6qfz4pqye0jchse 06-04 10:34:17.606: ich/Exception Auftrat, mit Fehler(19955): println needs a message
Schreibe einen Kommentar