在4.4之前的KitKat设备上没有接收推送

时间:2016-01-04 13:32:08

标签: android google-cloud-messaging

我正在使用GCM InstanceID Class来生成令牌。它在5.0或更高版本的设备上工作正常但在4.4之前的KitKat设备上我没有通过gcm推送。

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eliteappcreations.shouldigo">

    <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="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />
    <!--
 The ACCESS_COARSE/FINE_LOCATION 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" />


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


    <application
        android:name=".ShouldIGoApplication"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".activity.SplashScreenActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activity.LoginActivity"
            android:label="@string/title_activity_login"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar" />

        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <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="@string/google_maps_key" />
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/debug_facebook_app_id" />

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            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" />

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

        <service
            android:name=".service.gcmservice.GcmNotificationListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name=".service.gcmservice.NotificationInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
        <service
            android:name=".service.gcmservice.TokenRegistrationIntentService"
            android:enabled="true" />
    </application>

</manifest>

服务

 @Override
    protected void onHandleIntent(Intent intent) {

        Log.i(TAG, "service started");

        InstanceID instanceID = InstanceID.getInstance(TokenRegistrationIntentService.this);
        try {
            String token = instanceID.getToken(getString(R.string.gcm_sender_id),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE);
            Log.i(TAG, token);

            if (token != null) {
                sendRegistrationTokenToServer(token);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        stopSelf();
    }

click here for documentation

1 个答案:

答案 0 :(得分:1)

您可以在服务器端将delay_while_idle标记设置为false,以便在Lollipop下方运行GCM推送消息服务。

<强> delay_while_idle

如果包含,则表示如果设备空闲,则不应立即发送消息。服务器将等待设备变为活动状态,然后仅发送每个collapse_key值的最后一条消息。可选的。默认值为false,并且必须是JSON布尔值。

请参阅discussion了解更多