无法在旧版本4.1上安装的应用程序上收到Android GCM消息

时间:2014-08-28 19:20:05

标签: android push-notification google-cloud-messaging android-2.3-gingerbread

我无法在运行android<的应用上收到推送通知4.1

我有一个真正的设备运行KITKAT 4.4.4并且工作正常,但是使用旧设备(如固件2.3.6)它不起作用...我也在4.0.4上测试过没有运气。

我可以在服务器上注册和注销,我正确获取了注册ID,但是当我触发从服务器到旧设备的推送通知时,没有任何事情发生。

我在日志中看到的唯一消息是:

 W/GTalkService﹕ [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE cat=[com.metu.tracker.app] (has extras) }

我在下面发布了我的配置,因此您可以看到代码。

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.metu.tracker.app" >

<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Permission to save in external storage for image save (avatar) -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Permission to get fine coordinate -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<!-- Permission to open the gallery and modify photos -->
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!-- Permission to get MAPS -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<permission
    android:name="com.metu.tracker.app.permission.MAPS_RECEIVE"
    android:protectionLevel="signature"/>

<uses-permission android:name="com.metu.tracker.app.permission.MAPS_RECEIVE"/>

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

<!-- Permission GCM -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.metu.tracker.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.metu.tracker.gcm.permission.C2D_MESSAGE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.metu.tracker.app" />

        </intent-filter>
    </receiver>

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

    <activity
        android:name="com.metu.tracker.app.MainActivity"
        android:label="@string/app_name"
        android:noHistory="true">
  ...

这是我的接收者:

package com.metu.tracker.app;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;

/**
 * Created by metu on 28/08/14.
 */
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.
    int FLAG_INCLUDE_STOPPED_PACKAGES = 32;
    intent.addFlags(FLAG_INCLUDE_STOPPED_PACKAGES);
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
 }
}

这是我的服务:

public class GCMIntentService extends GCMBaseIntentService {

public GCMIntentService() {
    super(SENDER_ID);
}

/**
 * Method called on device registered
 **/
@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    //displayMessage(context, "Your device registred with GCM");
    //Log.d("NAME", MainActivity.name);
    ServerUtilities.register(context, registrationId);
}

/**
 * Method called on device un registred
 * */
@Override
protected void onUnregistered(Context context, String registrationId) {
    //Log.i(TAG, "Device unregistered");
    displayMessage(context, getString(R.string.gcm_unregistered));
    ServerUtilities.unregister(context, registrationId);
}

    /**
     * Method called on Receiving a new message
     * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");

    String message = intent.getExtras().getString("message");
    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);

    // Release the wake lock provided by the WakefulBroadcastReceiver.
    GcmBroadcastReceiver.completeWakefulIntent(intent);
...

我看起来已经在stackoverflow中,有一些有关此错误的有用信息,但我无法弄清楚如何解决它。我需要一些帮助。

感谢。

1 个答案:

答案 0 :(得分:3)

我发现了错误。

清单错了:

<!-- Permission GCM -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.metu.tracker.gcm.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.metu.tracker.gcm.permission.C2D_MESSAGE" />

我的应用包名称是com.metu.tracker.app,所以这里的行是错误的,正确的:

<!-- Permission GCM -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.metu.tracker.app.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="com.metu.tracker.app.permission.C2D_MESSAGE" />

BroadcastReceiver定义错误:

<receiver
    android:name=".GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND">
    <intent-filter>

正确的名称是:

<receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

更改后,推送通知将起作用。

如果不起作用,请取消注册并重新注册......