简体   繁体   中英

GCM doesn't receive messages in android 2.3.6 but works in android 4.1.2

I don't know where is the problem with my manifest, the problem is only with android 2.3.6 . In my tablet with android 4.1.2 it's receiving the messages. I have received the registration ID with 2.3.6 but the messages are never received. Please,someone could help me?:

<?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. The device register correctly in android 2.3.x but it doesn't receive the messages, my message log on onMessage isn't never called, while in android 4.1.x it works perfectly. Thanks for your help.

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){

}

}

Try following workaround not using (so far as I know) any deprecated classes. Add the action "com.google.android.c2dm.intent.REGISTRATION" to the GCMBroadcastReceiver in your manifest . This will enable to receive the registration_id at your 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>

After that your GCMBroadcastReceiver is able to receive the 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 */
   }
}

Although I still get a SERVICE_NOT_AVAILABLE error, I can handle the registration_id in my GCMBroadcastReceiver and I am able to send messages to my smartphone. Quite weird, but it works for me.

I was facing same problem from last one week but simple solutions is Android.Manifest File Changes.You have added only two permissions for C2D_MESSAGE but it required three.

<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" />

It will work for all android versions.

Ensure that your package name specified in the manifest matches the category name specified in the GCM receiver configuration. According to the manifest provided Your package name should look as below :

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

Similar question can be found here GCM Push RECEIVE category Name on Android OS < 4.1 . Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM