实际上我的应用程序工作正常并且能够接收GCM推送通知但是有些设备仅在我的应用程序处于后台时才在GCMIntentService中接收GCM通知。设备是huwai荣誉,xiaomi的一些模型。所以贝娄是我的代码,让我知道是否有人事先经历过这个问题。有人可以帮我这个吗?
Android清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.testapp"
android:installLocation="preferExternal"
android:versionCode="41"
android:versionName="4.1.7" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<permission
android:name="com.app.testapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.app.testapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_LOGS" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<application
android:name="com.app.testapp.MyApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/SampleTheme">
<activity
android:name="com.app.testapp.activity.SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/ActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- GCM -->
<receiver
android:name="com.app.testapp.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.app.testapp" />
</intent-filter>
</receiver>
<!-- Services -->
<service android:name="com.app.testapp.GCMIntentService" />
接收器:
public class GCMBroadcastReceiver extends
com.google.android.gcm.GCMBroadcastReceiver {
@Override
protected String getGCMIntentServiceClassName(Context context) {
return "com.app.testapp.GCMIntentService";
}
}
服务:
package com.app.testapp;
import android.content.Context;
import android.content.Intent;
import com.app.testapp.util.Constants;
import com.app.testapp.util.Logger;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService {
private static String TAG = "GCM";
public MyGCMIntentService() {
super(Constants.SENDER_ID);
}
@Override
protected void onError(Context arg0, String arg1) {
Logger.logger(TAG, "onError");
}
String message = null;
@Override
protected void onMessage(Context context, Intent intent) {
message = intent.getExtras().getString("message");
Logger.logger(TAG, "onMessage message=" + message);
}
@Override
protected void onRegistered(Context arg0, String arg1) {
Logger.logger(TAG, "onRegistered");
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Logger.logger(TAG, "onUnregistered");
}
}