仅当应用程序正在运行或在后台运行时才会触发GCM BroadcastReceiver

时间:2014-02-11 16:48:08

标签: android broadcastreceiver google-cloud-messaging

我已经实现了GCM服务并且一切正常但是当我关闭应用程序后我应该收到通知时(通过长按主页按钮然后在垃圾图标 - Galaxy S III),我不知道收到它。

这是我的 Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <!-- The following two permissions are not required to use
         Google Maps Android API v2, but are recommended. -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <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="com.google.android.c2dm.permission.RECEIVE" />
    <permission android:name="com.example.proj.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.example.proj.permission.C2D_MESSAGE" />


    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/projicon"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock"
        android:uiOptions="splitActionBarWhenNarrow" >
        <activity
            android:name="com.example.proj.FbActivity"
            android:label="@string/app_name"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:uiOptions="splitActionBarWhenNarrow">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
          <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
          <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="APIKEY"/>
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
        <activity android:name="com.facebook.LoginActivity"></activity>
        <activity android:name="MainActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"></activity>

        <receiver
            android:name="com.example.proj.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.proj" />
            </intent-filter>
        </receiver>
        <service android:name="com.example.proj.GcmIntentService" />
    </application>
</manifest>

GcmBroadcastReceiver.java:

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // 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);
    }

}

我不确定是否按照我杀死应用程序进程的方式关闭应用程序,或者它仍然应该在后台运行并接收那些masseges。

1 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,我发现我在清单文件中犯了一个错误:

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

应该是:

<permission android:name="<your app package>.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<uses-permission android:name="<your app package>.permission.C2D_MESSAGE" />

其中app包是包含扩展Application的类的包(应该在清单文件中指向)。

修好后,我已经能够强制关闭我的应用程序并仍然通过GCM接收推送通知。