Johanan Liebermann
Johanan Liebermann

Reputation: 821

GcmBroadcastReceiver onReceive method not fired on Android 4.0.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!

Upvotes: 3

Views: 11349

Answers (3)

Oscar Calderon
Oscar Calderon

Reputation: 81

I had the same problem recently, and discovered that it is a package name error in the permissions. need to follow this guide exactly, with the name of your main package, that the example of google lends itself to Confucianism.

Add the following to your application's manifest:

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"

And In the filter name:

A receiver for com.google.android.c2dm.intent.RECEIVE

Upvotes: 0

Eran
Eran

Reputation: 393831

You have <uses-permission android:name="com.jlieberman.nightwatch.permission.C2D_MESSAGE" /> in your manifest, but I don't see where you define this permission.

You should add :

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

Upvotes: 4

Vardhan D G
Vardhan D G

Reputation: 316

Good please try this

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

And for the wakelocker i wrote a class, here it is,

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

Try this and if it doesn't work ill be here to help!!!!

Upvotes: 1

Related Questions