2013-06-24 48 views
0

我正在使用GCM庫在我的Android應用程序中實現推送通知。 問題是,即使認爲,服務器端(發送GCM消息)和Android註冊都很好,似乎我的Android應用程序不會收到發送的消息! 我搜索了很多,試圖通過不同的方式解決它,但仍然沒有。所以,這是我的應用程序文件,並非常感謝您的幫助。Android應用程序中的GCM消息接收

  • 主要:

    public class Login extends Activity implements OnClickListener{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.activity_login); 
    btnGo = (Button) findViewById(R.id.btnenter); 
    btnGo.setOnClickListener(this); 
    } 
    public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.login, menu); 
    return true; 
    } 
    
    public void onClick(View v) { 
        if (v==btnGo){ 
         Intent intent = new Intent(Login.this, GCMIntentService.class); 
         intent.putExtra("val", "coucou"); 
         startService(intent); 
    
         Intent intent1 = new Intent(Login.this, MedicoAppliMain.class); 
         startActivity(intent1); 
           } 
        } 
    } 
    
  • Android清單:

    <?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.medicoappli" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    
    <uses-sdk 
    android:minSdkVersion="9" 
    android:targetSdkVersion="17" /> 
    
    <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.example.medicoappli.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 
    
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 
    <uses-permission android:name="com.example.medicoappli.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.USE_CREDENTIALS" /> 
    
    
    <application 
    android:name="com.example.medicoappli.MyApp" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    
    <activity 
        android:name="external.interfaces.Login" 
        android:label="@string/title_activity_login" > 
    <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="com.example.medicoappli" /> 
        </intent-filter> 
    </receiver> 
    <service android:process=":service" android:name="GCMIntentService"></service> 
    

  • GCMIntentService:

    package com.example.medicoappli; 
    
    import com.google.android.gcm.GCMBaseIntentService; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.util.Log; 
    import android.widget.Toast; 
    
    
    public class GCMIntentService extends GCMBaseIntentService { 
    
    private static final String TAG = "GCMIntentService";     
    
    public GCMIntentService() { 
        super("329261430680"); 
    } 
    
    
    private static void generateNotification(Context context, String message) { 
    
        Toast toast = Toast.makeText(context, "Vous avez un Messsage GCM", 3); 
        toast.show(); 
    } 
    
    @Override 
    protected void onError(Context arg0, String arg1) { } 
    
    @Override 
    protected void onMessage(Context arg0, Intent arg1) { 
    
        Log.d("GCM", "RECEIVED A MESSAGE"); 
        Log.d("val", arg1.getStringExtra("val")); 
        // Get the data from intent and send to notification bar 
        //generateNotification(arg0, arg1.getStringExtra("message")); 
    } 
    
    @Override 
    protected void onRegistered(Context arg0, String arg1) { } 
    
    @Override 
    protected void onUnregistered(Context arg0, String arg1) {  } 
    
    } 
    
    • 服務器端:

      package Server_Side; 
      
      import java.util.ArrayList; 
      
      import com.google.android.gcm.server.Message; 
      import com.google.android.gcm.server.MulticastResult; 
      import com.google.android.gcm.server.Result; 
      import com.google.android.gcm.server.Sender; 
      import java.net.Authenticator; 
      import javax.xml.ws.BindingProvider; 
      
      
      
      class Notify { 
      public static void main(String args[]) { 
      try { 
      Sender sender = new Sender("AIzaSyDjIcQt6mff7ujIvqWtwKVuVEYmbUBy0I0"); 
      
          // use this line to send message with payload data 
          Message message = new Message.Builder() 
            .collapseKey("1") 
            .timeToLive(3) 
            .delayWhileIdle(true) 
            .addData("message","this text will be seen in notification bar !!") 
            .build(); 
      
          // Use this code to send to a single device (clé associé à l'application) 
          Result result = sender.send(message,"APA91bGJxJxgtjmdKV1Ws766Cwr8ZaVKSn0Q7F17OQccI8sGhDWYo2pJCW0znS6qfEsy5ui1CSq9_ihGy9UKvoV9yqQW3AqP1c25ghLLBqt6nsPGEJE0qqHMhqIrhvVVDRy29R0gqtVZV-kJmicW5K8T_zOFO9reRUeADqTcxedKyjHhwFkxHOA",1); 
      
          System.out.println(result.toString()); 
      
      } 
           catch (Exception e) { 
          e.printStackTrace(); 
      
      } 
      
      } 
      } 
      

感謝。

回答

0

我沒有看到任何代碼將您的應用程序註冊到GCM並將註冊ID傳遞到您的服務器。

我看不出在onClick啓動意圖服務點:

Intent intent = new Intent(Login.this, GCMIntentService.class); 
intent.putExtra("val", "coucou"); 
startService(intent); 

這種服務只能由GCMBroadcastReceiver啓動時,無論是註冊或消息到達。

編輯:

我也會改變,從清單中的服務項:

<service android:process=":service" android:name="GCMIntentService"></service>

到:

<service android:name="com.example.medicoappli.GCMIntentService"></service>

或至少:

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

+0

事實上,由於代碼的清晰度,我抑制了那部分代碼。但是「new Sender()」和「result.send()」中使用的參數已經是應用程序註冊到GCM的結果。 而且,關於「GCMBroadcastReceiver」,「GCMIntentService」實現了應該啓動服務的onMessage函數,但它沒有。所以,我想要通過自編碼來啓動它。它也沒有工作! – Vtik

+0

@Nyassin您在服務器上從Google獲得什麼結果? – Eran

+0

@Nyassin如果Google返回成功響應(帶有消息ID),請參閱我的更新回答,以獲取您的清單中建議的更改。 – Eran

相關問題