1

I am trying to get a msg to my app from a server. I have this server url wich sends msg to all users: https://taub-prayer-ramym.c9.io/send?message=123

my app's token is registered in the server... but I have a problem that the onMessageReceived() method from my GcmListenerService implementation isn't called.

my Manifest:

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    <uses-permission android:name="com.example.atheel.prayer.permission.C2D_MESSAGE"/>
    <uses-permission android:name="com.google.android.c2dm.permission.SEND"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

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

        <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.example.atheel.prayer" />
            </intent-filter>
        </receiver>
        <service
            android:name="com.example.atheel.prayer.MyGcmListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name="com.example.atheel.prayer.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID"/>
            </intent-filter>
        </service>
        <service android:exported="false" android:name="com.example.atheel.prayer.RegistrationIntentService"/>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".EmptyStatus"
            android:label="@string/title_activity_empty_status" >
        </activity>
        <activity
            android:name=".UnEmpty"
            android:label="@string/title_activity_un_empty" >
        </activity>
    </application>

</manifest>

my myGCMListener :

public class MyGcmListenerService extends GcmListenerService {
    private static final String TAG = "MyGcmListenerService";
    File notificationsFile;
    private String sessionForAppClosed;

    // [START receive_message]
    @Override
    public void onMessageReceived(String from, Bundle data) {
        Log.i(TAG,"Message Received!!!!!!!!!");
        System.out.print("***************Message Received!!!!!!!!!");
    }
}

anyone can think why its not working?

Atheel
  • 187
  • 1
  • 3
  • 16

1 Answers1

2

I suppose that you already read the official documentation for GCM support. I also suggest you to read this article.

Compared to my GCM code, you are missing the line

<action android:name="com.google.android.c2dm.intent.GCM_RECEIVED_ACTION"/> 

from class GcmReceiver. I also don't see meta tag about the gcm version:

<meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

Third thing that I do differently is the specification of permission:

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    <permission
    android:name="your.package.permission.C2D_MESSAGE"
    android:protectionLevel="signature"/>

I also remember that on some networks the gcm simply do not work. It's a weird thing, but you can read more about it here.

Edit: since the issue is still not resolved, I add my working code for receiving GCM below. Note that I suppose that the app is successfully registered in google services, firewall is not blocked and the app server successfully send the message to google servers: GcmBroadcastReceiver - for receiving the CGM messages

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    ComponentName comp = new ComponentName(context.getPackageName(),GcmIntentService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

}

This component calls following service

public class GcmIntentService extends IntentService {

public GcmIntentService() {
    super(App.getStr(R.string.gcm_sender_id)); // this integer is provided when registering the application 
}

@Override
protected void onHandleIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
    String messageType = gcm.getMessageType(intent);
    if (!extras.isEmpty()) {
        if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
            sendNotification(extras);
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
            sendNotification(extras);
        } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            // here you can do your work
            }
        }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
}

}

PS: note that my solution is heavily inspired by other solutions on the internet, for example this one


Community
  • 1
  • 1
vanomart
  • 1,739
  • 1
  • 18
  • 39
  • thank you. 1) the missing line supposed to be in the receiver or service? 2) what you type instead of signature ? – Atheel Aug 29 '15 at 12:58
  • the missing line is in receiver and "signature" is not a placeholder, it should be as it is: "signature". Check it here http://developer.android.com/intl/ru/guide/topics/manifest/permission-element.html PS: the only placeholder in the code given is your.package - this should be your base package, which should be com.example.atheel.prayer – vanomart Aug 29 '15 at 13:51
  • I posted an edit. If the problem still preserves, feel free to contact me via my website martinvano.sk . I am sure we can fix it somehow. – vanomart Aug 29 '15 at 19:58
  • @vanomart i think you're still on the old APIs – Michael Obi Dec 21 '15 at 07:30