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.