3

I am writing an Android 4.0.3 program with GCM support. The app registers fine and sending a message from the server also seems to work (getting "success" from Google with a message id), however the onReceive method on my BroadcastReceiver doesn't get fired for some reason.

Here is the manifest:

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <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.jlieberman.nightwatch.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.jlieberman.nightwatch.NightWatch"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

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

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

        <service android:name=".GcmIntentService" />
    </application>

</manifest>

And here is the BroadcastReceiver:

package com.jlieberman.nightwatch;

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

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);
    }

}

Any idea what is causing the problem? Thanks!

4

3 回答 3

4

<uses-permission android:name="com.jlieberman.nightwatch.permission.C2D_MESSAGE" />在清单中有,但我看不到您在哪里定义此权限。

您应该添加:

<permission android:name="com.jlieberman.nightwatch.permission.C2D_MESSAGE" android:protectionLevel="signature" />

于 2013-09-25T14:34:19.083 回答
1

好请试试这个

   import com.google.android.gms.gcm.GoogleCloudMessaging;
    import android.app.Activity;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.support.v4.app.NotificationCompat;
    import android.util.Log;

    public class MyBroadcastReceiver extends BroadcastReceiver {
        static final String TAG = "pushnotification";
        public static final int NOTIFICATION_ID = 1;
        private NotificationManager mNotificationManager;
        NotificationCompat.Builder builder;
        Context ctx;
        @Override
        public void onReceive(Context context, Intent intent) {
            GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
            ctx = context;
            WakeLocker.acquire(ctx);
            String messageType = gcm.getMessageType(intent);
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                sendNotification("Send error: " + intent.getExtras().toString());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                sendNotification("Deleted messages on server: " +
                        intent.getExtras().toString());
            } else {
                sendNotification("Received: " + intent.getExtras().toString());
            }
            setResultCode(Activity.RESULT_OK);
            WakeLocker.release();
        }
}

对于唤醒锁,我写了一个类,在这里,

WakeLocker.java

import android.content.Context;
import android.os.PowerManager;

public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context context) {
        if (wakeLock != null) wakeLock.release();

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "WakeLock");
        wakeLock.acquire();
    }

    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

试试这个,如果它不起作用,我会在这里提供帮助!!!!

于 2013-09-25T12:57:28.257 回答
0

我最近遇到了同样的问题,发现是权限中的包名错误。需要准确地遵循本指南,并使用您的主要包的名称,谷歌的例子适合儒家思想。

将以下内容添加到应用程序的清单中:

The com.google.android.c2dm.permission.RECEIVE
The android.permission.INTERNET
The android.permission.GET_ACCOUNTS
The android.permission.WAKE_LOCK
An applicationPackage + ".permission.C2D_MESSAGE"

在过滤器名称中:

A receiver for com.google.android.c2dm.intent.RECEIVE
于 2014-05-08T15:22:19.303 回答