1

Мой клиент GCM может регистрироваться на всех версиях, но сообщения принимаются на android версии 4.x, а не на android 2.3 .6. Я внес много изменений, но я не могу понять эту проблему, пожалуйста, помогите мне, спасибо заранее.GCM-сообщения не получены на android 2.3.6 v, но отлично работают на android 4.X v

манифеста код файла

<?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; 

    } 
} 
+1

Установили ли вы учетную запись Google на устройствах Android 2.3.6? Из документов [GCM docs] (http://developer.android.com/google/gcm/client.html): на устройствах Android GCM использует существующее соединение для служб Google. Для устройств с предустановкой 3.0 требуется, чтобы пользователи настраивали свои учетные записи Google на своих мобильных устройствах. Учетная запись Google не является обязательным требованием для устройств под управлением Android 4.0.4 или новее. – Koh

+0

Да, я уже установил учетную запись google, и на устройстве есть игровые сервисы. Есть ли проблемы с последними обновленными GCM? – user3918105

+0

Какой ответ вы получаете на своем сервере приложений GCM? Ответ должен иметь успешный ответ для всех, включая клиентов Android 2.3.6. Если нет, должна быть какая-то информация в тексте ответа относительно корня проблемы. – Koh

ответ

0

Если вы используете ароматы продукта, возможно, у вас такая же проблема, как я нашел.

Я обнаружил, что при использовании ароматизаторов с различными приложениями, ваш пакет в файле AndroidManifest для разрешения и категории неверен. И это не имеет значения для Android 5.x, но для Android 2.3 это означает, что широковещательный приемник никогда не будет вызван.

Решение, которое я нашел, это использование градиента 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> 
0

Использование Последняя зависимость/библиотека GCM для SDk версии 4 или heigher

compile "com.google.android.gms:play-services-gcm:10.2.0" 

в build.gra dle file в зависимости. и добавление файла уровне build.gragle

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

своих работ для hiegher версий.