当应用程序空闲一段时间时,GCM接收器无法工作,但在应用程序打开时工作正常

时间:2015-05-15 17:00:03

标签: android google-cloud-messaging

我已在我的应用程序中实现了GCM推送通知,它运行正常。应用程序处于前台或后台时接收器接收。但是,当应用程序处于后台时,Receiver没有收到某些特定的手机。我做错了什么?

的AndroidManifest.xml

    <uses-sdk
    android:minSdkVersion="12"
    android:targetSdkVersion="22" />

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

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

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

 <application
    android:allowBackup="true"
    android:icon="@drawable/logo"
    android:label="@string/app_name"
    android:hardwareAccelerated="true"
    android:theme="@style/AppTheme" 
    android:killAfterRestore="false"        
    android:allowClearUserData="false">

 <receiver
        android:name="com.testapp.ExternalReceiver"
        android:exported="true"
        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" />
            <action android:name="com.google.android.c2dm.intent.REGISTER" />
            <action android:name="com.testapp" />

            <category android:name="com.testapp" />
        </intent-filter>
    </receiver>

    <service android:name="com.testapp.GCMService" />

 </application>

ExternalReceiver

public class ExternalReceiver extends WakefulBroadcastReceiver  {

    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
             ComponentName comp = new ComponentName(context.getPackageName(),
                        GCMService.class.getName());

             GCMService.context = context;
                // Start the service, keeping the device awake while it is launching.
                startWakefulService(context, (intent.setComponent(comp)));

                setResultCode(Activity.RESULT_OK);      
        }
    }

}

1 个答案:

答案 0 :(得分:0)

你必须改变:

<receiver
        android:name="com.testapp.ExternalReceiver"
        android:exported="true"
        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" />
            <action android:name="com.google.android.c2dm.intent.REGISTER" />
            <action android:name="com.testapp" />

            <category android:name="com.testapp" />
        </intent-filter>
    </receiver>

对此:

<receiver
        android:name=".GcmBroadcastReceiver"
        android:exported="true"
        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" />
            <action android:name="com.google.android.c2dm.intent.REGISTER" />
            <action android:name="com.testapp" />

            <category android:name="com.testapp" />
        </intent-filter>
    </receiver>

只需一行!!!