我不知道我的清单问题出在哪里,问题只在于 android 2.3.6.在我的 Android 4.1.2平板电脑中,它正在接收消息.我收到了2.3.6的注册ID,但是从未收到过这些消息.请有人帮我吗?: ?xml version="1.0"
<?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>
UPDATE
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){ }
}
尝试以下解决方法,不要使用(据我所知)任何已弃用的类.将动作“com.google.android.c2dm.intent.REGISTRATION”添加到清单中的GCMBroadcastReceiver.这将使您能够在GCMBroadcastReceiver上接收registration_id.<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错误,但我可以处理GCMBroadcastReceiver中的registration_id,并且我可以向我的智能手机发送消息.很奇怪,但它对我有用.