【问题标题】:GCM messages Reception in Android ApplicationAndroid 应用程序中的 GCM 消息接收
【发布时间】:2013-06-26 08:35:53
【问题描述】:

我正在使用 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 清单:

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

谢谢。

【问题讨论】:

    标签: google-cloud-messaging


    【解决方案1】:

    我没有看到将您的应用注册到 GCM 并将注册 ID 传递到您的服务器的任何代码。

    我看不出在onClick 中启动意图服务的意义:

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

    此服务只能由GCMBroadcastReceiver 在注册或消息到达时启动。

    编辑:

    我还会更改清单中的服务条目:

    &lt;service android:process=":service" android:name="GCMIntentService"&gt;&lt;/service&gt;

    到:

    &lt;service android:name="com.example.medicoappli.GCMIntentService"&gt;&lt;/service&gt;

    或至少:

    &lt;service android:name=".GCMIntentService"&gt;&lt;/service&gt;

    【讨论】:

    • 事实上,由于代码清晰,我隐藏了执行此操作的代码部分。但是这里“new Sender()”和“result.send()”中使用的参数已经是应用注册到GCM的结果了。而且,关于“GCMBroadcastReceiver”,“GCMIntentService”实现了应该启动服务的 onMessage 函数,但它没有。所以,我想通过自编码来启动它。它也没有工作!
    • @NYassin 你在服务器上从 Google 得到什么结果?
    • @NYassin 如果 Google 返回成功响应(带有消息 id),请参阅我的更新答案以获取清单中的建议更改。
    • 这是服务器响应(Sout):“[ messageId=0:1372234454309956%7f0e114400000031 ]”。所以我认为我的服务器没问题,消息确实离开了这一边,但未被检测到在我的 Android 应用程序中。我也尝试了您编辑的代码,但它也没有工作。
    猜你喜欢
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多