2

Apps like whatsap receives notification even when the app is not opened by the user. But in my case it is not happening. I am receiving notification on a class which inherits from GcmListenerService like below

public class GCMPushReceiverService extends GcmListenerService {


//This method will be called on every new message received
@Override
public void onMessageReceived(String from, Bundle data) {
    //Getting the message from the bundle
    String message = data.getString("message");


    Log.e("msg",message);

    //Displaying a notiffication with the message
     setNotification(message);
    }
}

I found there is something like GCMBroadcastReceiver or WakefulBroadcastReceiver that might help me. But i am confused about its implementation. If i can receive the message inside the onReceive() method of WakefulBroadcastReceiver, then why should i use GcmListenerService? Can anybody help me to implement it in the proper way?

My manifest file is as below

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

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

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.localmarket.permission.C2D_MESSAGE" />
 <application
    android:name="com.example.app"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"

   <activity android:name=".SplashActivity"
         android:theme="@style/Theme.Transparent"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   <!--   GCM Receiver -->
    <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" />

            <!--<category android:name="com.gnirt69.gcmexample" />-->
        </intent-filter>
    </receiver>

    <!--             GCM Receiver Service -->
    <service
        android:name=".GCMPushReceiverService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>



    <!--             GCM Registration Intent Service -->
    <service
        android:name=".GCMRegistrationIntentService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID" />
        </intent-filter>
    </service>


</application>
</manifest>

I think there should be some backend changes for fcm. Thus i cant migrate to fcm for now.

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55
  • Possible duplicate of [Push notification works incorrect when app is on background or not running](http://stackoverflow.com/questions/37876257/push-notification-works-incorrect-when-app-is-on-background-or-not-running) – Tim Jul 14 '16 at 21:52
  • try to migrate to FCM, it's easy to use, and very simple – Faruk Jul 14 '16 at 22:03
  • i think there should be some backend changes for fcm. Thus i cant use it for now. So can u please help me on this issue? – Hari Krishnan Jul 15 '16 at 04:45
  • 1
    GCM and FCM app server are identical. The only thing you can change is the url you make a request to, but you don't even have to – Tim Jul 15 '16 at 07:41
  • There are no required back end changes to start using FCM on the client side. GCM on the server side can send messages to a client that is using FCM. – Arthur Thompson Jul 15 '16 at 16:21

0 Answers0