2012-09-14 58 views
0

我想從一天多的時間裏使用gcm實現一個簡單的PUSH通知,但無法實現它。如何在Android中實現推送通知

當過我加入這個權限

<permission android:name="Android.Test.permission.C2D_MESSAGE" android:protectionLevel="signature" /> 

我收到此錯誤信息,同時通過移動設備調試

pkg: /data/local/tmp/Android.Test 
Failure [INSTALL_PARSE_FAILED_MANIFEST_MALFORMED] 

有人建議我我在做什麼。爲進一步幫助我把我的整個manifest.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
      package="Android.Test" 
      android:versionCode="1" 
      android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="14"/> 

    <permission android:name="Android.Test.permission.C2D_MESSAGE" android:protectionLevel="normal" /> 
    <uses-permission android:name="Android.Test.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 



    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> 
     <activity android:name="MainActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 
       <category android:name="android.intent.category.LAUNCHER"/> 
      </intent-filter> 
     </activity> 
     <receiver 
       android:name="com.google.android.gcm.GCMBroadcastReceiver" 
       android:permission="com.google.android.c2dm.permission.SEND" > 
      <intent-filter> 
       <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
       <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 

       <category android:name="Android.Test" /> 
      </intent-filter> 
     </receiver> 
     <service android:name=".GCMIntentService" /> 
    </application> 
</manifest> 

編輯1: 這是我想從C#發送郵件到我的Android設備.....我發現這個代碼從博客。

private string SendGCMNotification(string apiKey, string postData, string postDataContentType = "application/json") 
     { 

      ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateServerCertificate); 

      // 
      // MESSAGE CONTENT 
      byte[] byteArray = Encoding.UTF8.GetBytes(postData); 

      // 
      // CREATE REQUEST 
      HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://android.googleapis.com/gcm/send"); 
      Request.Method = "POST"; 
      Request.KeepAlive = false; 
      Request.ContentType = postDataContentType; 
      Request.Headers.Add(string.Format("Authorization: key={0}", apiKey)); 
      Request.ContentLength = byteArray.Length; 

      Stream dataStream = Request.GetRequestStream(); 
      dataStream.Write(byteArray, 0, byteArray.Length); 
      dataStream.Close(); 

      // 
      // SEND MESSAGE 
      try 
      { 
       WebResponse Response = Request.GetResponse(); 
       HttpStatusCode ResponseCode = ((HttpWebResponse)Response).StatusCode; 
       if (ResponseCode.Equals(HttpStatusCode.Unauthorized) || ResponseCode.Equals(HttpStatusCode.Forbidden)) 
       { 
        var text = "Unauthorized - need new token"; 

       } 
       else if (!ResponseCode.Equals(HttpStatusCode.OK)) 
       { 
        var text = "Response from web service isn't OK"; 
       } 

       StreamReader Reader = new StreamReader(Response.GetResponseStream()); 
       string responseLine = Reader.ReadToEnd(); 
       Reader.Close(); 

       return responseLine; 
      } 
      catch (Exception e) 
      { 

      } 
      return "error"; 
     } 

這是我得到的responseLine代碼

{"multicast_id":7357801824672,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]} 

感謝

+0

http://www.basic4ppc.com/forum/basic4android-getting-started-教程/ 19226-android-push-notification-gcm-framework-tutorial.html –

+2

應該是你的包名錯誤。不要在包名中使用'capital'。嘗試重命名爲'com.android.test',看看你是否可以安裝 – Aprian

+0

Aprian,我遵循你的建議,現在它的工作,我能夠編譯和運行在手機上的應用程序,但我無法接收在電話上留言。我從c#.net推送消息,但他們沒有打電話給任何建議......請...... – Shax

回答

1
try this one  

<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/> 


<uses-permission android:name="android.permission.INTERNET" /> 


<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 


<uses-permission android:name="android.permission.WAKE_LOCK" /> 


<permission 
    android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 
<uses-permission 
    android:name="com.google.android.gcm.demo.app.permission.C2D_MESSAGE" /> 

<!-- This app has permission to register and receive data message. --> 
<uses-permission 
    android:name="com.google.android.c2dm.permission.RECEIVE" /> 

<!-- Main activity. --> 
<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 


    <receiver 
     android:name="com.google.android.gcm.GCMBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <!-- Receives the actual messages. --> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <!-- Receives the registration id. --> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
      <category android:name="Android.Test" /> 
     </intent-filter> 
    </receiver> 


    <service android:name=".GCMIntentService" /> 
</application> 

+0

現在我能夠編譯但執行時我得到這個錯誤「java.lang.IllegalStateException:應用程序沒有定義權限Android.Test.permission.C2D_MESSAGE」 – Shax

+0

<使用的許可 機器人:名稱=「Android.Test.permission.C2D_MESSAGE」 /> – 2012-09-14 03:48:46

+0

添加上述代碼清單文件 – 2012-09-14 03:49:33

0

你的響應代碼包含了答案,你逝去的無效registration_ids

"results":[{"error":"InvalidRegistration"}] 

示例PHP代碼:

$postdata = array(
     'registration_ids' => array('your_device_registration_key'), 
     'data' => 'message' 
    ); 

    $headers = array(
     'Authorization: key=' . YOUR_API_KEY, 
     'Content-Type: application/json' 
    );