当设备处于睡眠模式时,BroadcastReceiver不从IntentService接收广播

时间:2015-01-29 07:39:14

标签: android broadcastreceiver google-cloud-messaging android-notifications

我有一个GCMBroadcastReceiver,它接收GCM广播并启动intentservice

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver{

    public static final String TAG = "GCMBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e(TAG, "Recieved GCM broadcast, starting GCM intent service");

        // Explicitly specify that GcmIntentService will handle the intent.
        ComponentName comp = new ComponentName(context.getPackageName(),
                GCMIntentService.class.getName());

        startWakefulService(context, (intent.setComponent(comp)));
        setResultCode(Activity.RESULT_OK);
    }
}

在GCMBroadcastReceiver上调用completeWakefulIntent之前,IntentService正在发送广播(广播接收器是GCMNotificationObserver)

protected void onHandleIntent(Intent intent) {
        if (intent == null) {
            return;
        }

        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);

        Log.e(TAG, "GCM message type: " + messageType);

        if (!extras.isEmpty()) { 

            if (GoogleCloudMessaging.
                    MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                onError(intent);
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_DELETED.equals(messageType)) {
                onDeletedMessages(intent);
            } else if (GoogleCloudMessaging.
                    MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                onMessage(intent);
            }
        }

        GCMBroadcastReceiver.completeWakefulIntent(intent);
    }

在onMessage方法中我播放

protected void onMessage(Intent intent) {
        String payload = intent.getStringExtra("cm_data");
        Intent broadCastIntent = new Intent(
                GCMNotificationObserver.ACTION_GCM_NOTIFICATION_MESSAGE_RECEIVED);
        broadCastIntent.putExtra("payload", payload);
        sendBroadcast(broadCastIntent);
    }

我面临的问题是GCMNotificationObserver没有收到广播。

我怀疑设备在收到广播之前正在返回睡眠模式。

更新

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package=“com.test.push"

    android:versionCode="126"

    android:versionName=“1.0.0.0" >



    <uses-sdk

        android:minSdkVersion="14"

        android:targetSdkVersion="21" />



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

    <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />

    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />

    <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />

    <uses-permission android:name="android.permission.READ_SYNC_STATS" />

    <uses-permission android:name="com.android.vending.BILLING" />



    <permission

        android:name=“com.test.push.permission.C2D_MESSAGE"

        android:protectionLevel="signature" />



    <uses-permission android:name="com.test.push.permission.C2D_MESSAGE" />

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

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-permission android:name="android.permission.GET_TASKS" />

    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

    <uses-permission android:name="android.permission.READ_CONTACTS" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.READ_CALENDAR" />



    <application

        android:name="com.test.android.GlobalData"

        android:allowBackup="true"

        android:hardwareAccelerated="true"

        android:icon="@drawable/app_icon"

        android:label="@string/app_name"

        android:logo="@drawable/ab_icon"

        android:theme="@style/MainTheme" >

        <meta-data

            android:name="com.google.android.gms.version"

            android:value="@integer/google_play_services_version" />

        <activity

            android:name=“com.test.push.TestActivity"

            android:label="@string/app_name" >

        </activity>

        <receiver android:name="com.test.push.observers.NetworkStateReceiver" >

            <intent-filter>

                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />

            </intent-filter>

        </receiver>

        <receiver

            android:name=".GCMBroadcastReceiver"

            android:permission="com.google.android.c2dm.permission.SEND" >

            <intent-filter>

                <action android:name="com.google.android.c2dm.intent.RECEIVE" />

                <category android:name="com.test.push" />

            </intent-filter>

        </receiver>

        <receiver

            android:name="com.test.push.observers.GCMNotificationObserver"

            android:exported="false" >

            <intent-filter>

                <action android:name="com.test.push.observers.GCM_REGISTER" />

                <action android:name="com.test.push.observers.GCM_MESSAGE_RECEIVED" />

                <action android:name="com.test.push.observers.GCM_MESSAGE_DELETED" />

                <action android:name="com.test.push.observers.GCM_NOTIFICATION_DISMISS" />



                <category android:name="com.test.push" />

            </intent-filter>

        </receiver>

        <service android:name=".GCMIntentService" />

    </application>



</manifest>

0 个答案:

没有答案