在Android 2.3.6 v上没有收到GCM消息,但在android 4.X v上工作正常

时间:2015-03-08 13:27:03

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

我的GCM客户端能够在所有版本上注册,但消息是在4. strong版本以上的 android上接收的,而不是在Android 2.3.6 上。我已经做了很多改变,但我无法解决这个问题请帮助我,提前谢谢。

清单文件代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.democlientapp"
    android:versionCode="1"
    android:versionName="1.0" >

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


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

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

    <permission android:name="com.democlientapp.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.democlientapp.permission.C2D_MESSAGE" />



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>

        <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>



        <activity
            android:name="com.democlientapp.RegisterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.democlientapp.MainActivity"
            android:label="@string/app_name" >

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




        <service android:name="com.democlientapp.GcmIntentService" />
    </application>

</manifest>

GCMBrodcastReceiver.java

package com.democlientapp;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("tag", "****************GcmBroadcastReceiver.java ok*************");
        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GcmIntentService.class.getName());
        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);

    }


}

GCMIntentService.java

package com.democlientapp;

import android.app.IntentService;
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.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GcmIntentService extends IntentService {
    public static  int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    String TAG="tag";
    SharedPreferences sp;
    StringBuffer sb;

    public GcmIntentService() {

        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.i("tg", "intent sservice******************");
        String extras1 = intent.getExtras().getString("data");
        //Log.i("tag", "string extras :"+extras1.toString());
        Bundle extras=intent.getExtras();
        //Log.i("tag", "bundle extras :"+extras);

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {  // has effect of unparcelling Bundle
               Log.i("tag", "entered");
            /*
             * Filter messages based on message type. Since it is likely that GCM
             * will be extended in the future with new message types, just ignore
             * any message types you're not interested in, or that you don't
             * recognize.
             */
            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + extras.toString());
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        extras.toString());
            // If it's a regular GCM message, do some work.
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                // This loop represents the service doing some work.
                for (int i=0; i<5; i++) {
                    Log.i(TAG, "Working... " + (i+1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                Log.i(TAG, "Received: " + extras.toString());
                // Post notification of received message.
                sendNotification("Received: " + extras);

            }
        }
        //Log.i(TAG, "Received11111111111111111111");
        //sendNotification("Received: registeration "+extras);
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        //Log.i(TAG, "Received22222222222222222222222");
        GcmBroadcastReceiver.completeWakefulIntent(intent);
        //Log.i(TAG, "Received3333333333333333");
    }

    // Put the message into a notification and post it.
    // This is just one simple example of what you might choose to do with
    // a GCM message.
    private void sendNotification(String msg) {
        Log.i("tag","GcmBroadcastReceiver.java send notification");
        //storing message in shared preference
        StoreMsg(msg);



        mNotificationManager = (NotificationManager)
                this.getSystemService(Context.NOTIFICATION_SERVICE);

        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("Spy notification")
        .setStyle(new NotificationCompat.BigTextStyle()
        .bigText(msg))
        .setContentText(msg);

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
        //mNotificationManager.no
        //NOTIFICATION_ID++;
    }

    public void StoreMsg(String Msg)
    {
        String  name="MyPrefs";
         sp = this.getSharedPreferences(name, Context.MODE_PRIVATE);
          sb=new StringBuffer();
         String mg = sp.getString("MESSAGES", "false");

         if (mg.equals("false")) {
         //sb.append(mg);
         sb.append(Msg);
         }
         else{
             sb.append(mg+"\n****************");
             sb.append("\n"+Msg);

         }

        Log.i("tag", "storemsg is: "+sb);
        Editor editor = sp.edit();
         editor.putString("MESSAGES",sb.toString());
         //Log.i("tag","Gcmintentservice.java,MESSAGES storing SharedPreferences");
         editor.commit();
         //Log.i("tag", "registeractivity.java, onclick method client username exists in DB");



    }
    public String getMsg(){
        String  name="MyPrefs";
         sp = this.getSharedPreferences(name, Context.MODE_PRIVATE);
        String prefName = sp.getString("MESSAGES", "false");
        Log.i("tag", "storemsg is: "+sb);
        return prefName;

    }
}

2 个答案:

答案 0 :(得分:0)

如果您使用的是产品口味,可能会遇到与我发现的问题相同的问题。

我发现当使用不同的applicationId的flavor时,那么你的包在AndroidManifest文件中的权限和类别是不正确的。 这对于Android 5.x无关紧要,但对于Android 2.3会导致广播接收器永远不会被调用。

我找到的解决方案是使用gradle&#39; placeholder support。使用 $ {applicationId} ,解决了所有Android版本的问题。

<强>的build.gradle

productFlavors {
    prod {
        applicationId 'com.democlientapp'
    }
    dev {
        applicationId 'com.democlientapp.dev'
    }
}

<强>的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.democlientapp"
    android:versionCode="1"
    android:versionName="1.0" >

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


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

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

    <permission android:name="${applicationId}.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="${applicationId}.permission.C2D_MESSAGE" />



    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>

        <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>



        <activity
            android:name="com.democlientapp.RegisterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.democlientapp.MainActivity"
            android:label="@string/app_name" >

        </activity>
         <receiver
            android:name="com.democlientapp.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="${applicationId}" />
            </intent-filter>
        </receiver>    

        <service android:name="com.democlientapp.GcmIntentService" />
    </application>

</manifest>

答案 1 :(得分:0)

对于sdk版本4或更高版本

使用GCM的最新依赖项/库
 compile "com.google.android.gms:play-services-gcm:10.2.0"

在依赖的build.gradle文件中。 并添加build.gragle级别文件

classpath 'com.google.gms:google-services:3.0.0'

它适用于更为严格的版本。