astReceiver只有在应用程序正在运行或在后台运行时才会触

astReceiver只有在应用程序正在运行或在后台运行时才会触

本文介绍了GCM BroadcastReceiver只有在应用程序正在运行或在后台运行时才会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

以下是我的 Manifest.xml

 <?xml version =1.0encoding =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/>
<使用权限android:name =com.google.android.providers.gsf.permission.READ_GSERVICES/>
< uses-permission android:name =android.permission.CALL_PHONE/>
Google Maps Android API v2,但建议您使用。 - >
< 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/>
<使用权限android:name =android.permission.GET_ACCOUNTS/>
<使用权限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>

< 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.ApplicationIdandroid: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>
< 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 {
$ b $ @Override
public void onReceive(Context context,Intent intent){
/ /明确指定GcmIntentService将处理意图。
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
//启动服务,保持设备在启动时保持清醒状态。
startWakefulService(context,(intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}

}

我不确定是否按以我杀死应用程序的方式关闭应用程序,或者它仍然应该在后台运行并接收这些管理员。

解决方案

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

 < permission android:name =com.example.gcm.permission.C2D_MESSAGE
android:protectionLevel =signature/>
<使用权限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/>

其中应用程序包是包含扩展(并且应该在清单文件中指出)。



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


I've implemented GCM service and it all works fine but when I'm supposed to recieve a notification after I closed the app (By long click at home button and then at the garbage icon - Galaxy S III), I don't recieve it.

Here's my 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);
    }

}

I'm not sure if by closing the app by the way I did kills the app proccess or it still should be running at the background and recieve those masseges.

解决方案

I had the same problem, and I found out I made a mistake in the manifest file:

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

should be:

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

where the app package is the package that contains the class that extends Application (and should be pointed in the manifest file).

After fixing this, I've been able to force close my app and still receive push notifications via GCM.

这篇关于GCM BroadcastReceiver只有在应用程序正在运行或在后台运行时才会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:45