GCM在android 2.3.6中不接收消息,但在android 4.1.2中有效

时间:2013-09-24 07:33:05

标签: android google-cloud-messaging android-2.3-gingerbread

我不知道我的清单问题在哪里,问题仅在于android 2.3.6 。在我的Android 4.1.2平板电脑中,它正在接收消息。我收到了2.3.6的注册ID,但是从未收到过这些消息。拜托,有人可以帮助我吗?:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ciqtech.quicky.mobile.passenger"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="preferExternal">

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

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

<permission
    android:name="com.ciqtech.quicky.mobile.passenger.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="com.ciqtech.quicky.mobile.passenger.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

 <!-- 
    GCM CONFIGURATION
 -->


<permission android:name="com.ciqtech.quicky.mobile.passenger.permission.C2D_MESSAGE" 
    android:protectionLevel="signature"/>

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.ciqtech.quicky.mobile.passenger.permission.C2D_MESSAGE" />    

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


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="..." />
    <activity
        android:name="com.ciqtech.quicky.mobile.passenger.activities.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>

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />

    <activity android:name="com.facebook.LoginActivity" >
    </activity>

    <activity
        android:name="com.ciqtech.quicky.mobile.passenger.activities.PassengerActivity"
        android:label="@string/title_activity_passenger" >
    </activity>

    <activity
        android:name="com.ciqtech.quicky.mobile.passenger.activities.LoginActivity"
        android:label="@string/title_activity_login" >
    </activity>

   <!-- GCM
        RECEIVER
     -->
     <receiver
        android:name="com.ciqtech.quicky.mobile.passenger.broadcast_receivers.QuickyGCMBroadcastReceiver"
        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.ciqtech.quicky.mobile.passenger" />

        </intent-filter>
    </receiver>


    <service android:name=".services.GCMIntentService" android:enabled="true" />       

</application>

</manifest>

更新

QuickyGCMBroadcastReceiver

 package com.ciqtech.quicky.mobile.passenger.broadcast_receivers;

import com.ciqtech.quicky.mobile.passenger.services.GCMIntentService;
import com.google.android.gcm.GCMBroadcastReceiver;
import android.content.Context;
import android.util.Log;

public class QuickyGCMBroadcastReceiver extends GCMBroadcastReceiver {

final String TAG = "QuickyGCMBroadcastReceiver";

@Override
protected String getGCMIntentServiceClassName(Context context) {
    Log.d(TAG, "GET GCM INTENT SERVICE CLASS NAME");
    return GCMIntentService.class.getName();
}

}

GCMIntentService。设备在android 2.3.x中正确注册,但它没有收到消息,我的消息登录onMessage从未被调用过,而在android 4.1.x中它完美无缺。谢谢你的帮助。

package com.ciqtech.quicky.mobile.passenger.services;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.provider.Settings.Secure;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.ciqtech.quicky.mobile.passenger.R;
import com.ciqtech.quicky.mobile.passenger.activities.LoginActivity;
import com.ciqtech.quicky.mobile.passenger.activities.PassengerActivity;
import com.ciqtech.quicky.mobile.passenger.broadcast_receivers.Device;
import com.ciqtech.quicky.mobile.passenger.configs.GCMSettings;
import com.ciqtech.quicky.mobile.passenger.dto.NotificationDTO;
import com.ciqtech.quicky.mobile.passenger.utils.SessionManager;
import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gms.gcm.GoogleCloudMessaging;


public class GCMIntentService extends GCMBaseIntentService {
Context ctx;
SharedPreferences prefs;

final String TAG = "GCMIntentService";
NotificationCompat.Builder builder;

public GCMIntentService() {
    super(GCMSettings.GCM_SENDER);
}

@Override
public void onError(Context context, String errorId) {
    Log.d(TAG + " - onError", "Messaging registration error: " + errorId);
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    Log.d(TAG + "- onRecoverableError", "Received recoverable error: " + errorId);
    return super.onRecoverableError(context, errorId);
}

@Override
protected void onMessage(Context context, Intent intent) {
    String msg = intent.getExtras().getString( "msg" );

    Log.d(TAG, " - onMessage: " + msg);

    // [more code]
}

// Put the GCM message into a notification and post it.
private void sendNotification(String msg) {

}

@Override
public void onRegistered(Context context, String registrationId) {
    Log.d(TAG + " - onRegistered", "onRegistered()");
    prefs = context.getSharedPreferences("Quickytaxi-Notifications",0);
    String deviceID = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
    SessionManager session = new SessionManager(context);
    String uid = session.getUserDetails().get("id");
    Log.d(TAG, deviceID + " " + registrationId + " " + uid);
    Device.register(deviceID, registrationId, uid);
}

@Override
protected void onUnregistered(Context context, String s) {
    Log.d(TAG + "- onUnregistered", "onUnregistered()");
    String deviceID = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
    Device.unregister(deviceID);
}

public void notify(NotificationDTO notificationDTO, Intent intent){

}

}

3 个答案:

答案 0 :(得分:3)

尝试以下解决方法,不要使用(据我所知)任何已弃用的类。将&#34; com.google.android.c2dm.intent.REGISTRATION&#34; 操作添加到GCMBroadcastReceiver中的manifest。这样,您就可以registration_id收到GCMBroadcastReceiver

<receiver
   android:name="YOUR_PACKAGE_NAME.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="YOUR_PACKAGE_NAME" />
      </intent-filter>
</receiver>

之后,您的GCMBroadcastReceiver能够收到registration_id

public void onReceive(Context context, Intent intent) 
{
   String regId = intent.getExtras().getString("registration_id");

   if(regId != null && !regId.equals("")) 
   {
       /* Do what ever you want with the regId eg. send it to your server */
   }
}

虽然我仍然收到SERVICE_NOT_AVAILABLE错误,但我可以处理registration_id中的GCMBroadcastReceiver,我可以向智能手机发送消息。很奇怪,但它对我有用。

答案 1 :(得分:3)

我在上一周面临同样的问题,但简单的解决方案是Android.Manifest文件更改。您只为C2D_MESSAGE添加了两个权限,但它需要三个。

<permission android:name="com.ciqtech.quicky.mobile.passenger.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.ciqtech.quicky.mobile.passenger.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.C2D_MESSAGE" />

它适用于所有Android版本。

答案 2 :(得分:1)

确保清单中指定的包名称与GCM接收器配置中指定的类别名称匹配。根据提供的清单,您的包名称应如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.ciqtech.quicky.mobile.passenger"
 android:versionCode="1"
 android:versionName="1.0" >

类似的问题可以在GCM Push RECEIVE category Name on Android OS < 4.1找到。希望有所帮助。