GCM消息在Android应用程序中接收

时间:2013-06-24 14:40:10

标签: google-cloud-messaging

我正在使用GCM库在我的Android应用程序中实现推送通知。 问题是,即使想到,服务器端(发送GCM消息)和Android注册都很好,似乎我的Android应用程序没有收到发送的消息! 我搜索了很多,试图通过不同的方式来解决它,但仍然没有。所以,这是我的应用程序文件,非常感谢您的帮助。

  • 主要:

    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 Manifest:

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

感谢。

1 个答案:

答案 0 :(得分: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>