I applied gcm push notification in my application. Everything is working fine and I get notifications.
Problem:
- When the application is in the background or completed, I get 2 notifications at a time.
- When the application is in the foreground, it receives only one notification that I want.
An application should receive only one notification as a requirement, but, unfortunately, faces an undefined situation.
My code is below:
GCMPushReceiverService to receive the message.
public class GCMPushReceiverService extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
Log.e("MeSs",""+message);
sendNotification(this,message, "Traccar App");
sendNotification(message);
}
private void sendNotification(Context context, String notificationText,
String notificationTitle) {
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "");
wakeLock.acquire();
Intent intent = new Intent(this, Home.class);
intent.putExtra("ChatFragment", "newChatFound");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestCode = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context)
.setSmallIcon(R.drawable.bug_log_two)
.setColor(Color.RED)
.setContentTitle(notificationTitle)
.setContentText(notificationText)
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
wakeLock.release();
}
private void sendNotification(String message) {
Intent intent = new Intent(this, Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestCode = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noBuilder.build());
}
}
manifest file code:
<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.vk.trackeruser" />
</intent-filter>
</receiver>
<service
android:name=".Notification.GCMPushReceiverService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".Notification.GCMRegistrationIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
GCMRegistrationIntentService class:
public class GCMRegistrationIntentService extends IntentService {
public static final String REGISTRATION_SUCCESS = "RegistrationSuccess";
public static final String REGISTRATION_ERROR = "RegistrationError";
public static final String SenderId = "my id with numeric number ex 9897979";
public GCMRegistrationIntentService() {
super("");
}
@Override
protected void onHandleIntent(Intent intent) {
registerGCM();
}
private void registerGCM() {
Intent registrationComplete = null;
String token = null;
try {
InstanceID instanceID = InstanceID.getInstance(getApplicationContext());
token = instanceID.getToken(SenderId, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.w("GCMRegIntentService", "token:" + token);
String tokan = token;
registrationComplete = new Intent(REGISTRATION_SUCCESS);
registrationComplete.putExtra("token", token);
} catch (Exception e) {
Log.w("GCMRegIntentService", "Registration error");
registrationComplete = new Intent(REGISTRATION_ERROR);
}
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
}
GCMTokenRefreshListenerService class:
public class GCMTokenRefreshListenerService extends InstanceIDListenerService{
@Override
public void onTokenRefresh() {
Intent intent = new Intent(this, GCMRegistrationIntentService.class);
startService(intent);
}
}
Class for getting my token GCM:
in oncreate {
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){
String token = intent.getStringExtra("token");
StaticContents.Gcm_token=token;
Log.e("Token",""+token);
Toast.makeText(getApplicationContext(), "Registration token:" + token, Toast.LENGTH_LONG).show();
} else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){
} else {
}
}
};
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(ConnectionResult.SUCCESS != resultCode) {
if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.showErrorNotification(resultCode, getApplicationContext());
} else {
}
} else {
Intent itent = new Intent(this, GCMRegistrationIntentService.class);
startService(itent);
}
}
@Override
protected void onResume() {
super.onResume();
Log.w("MainActivity", "onResume");
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(GCMRegistrationIntentService.REGISTRATION_SUCCESS));
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(GCMRegistrationIntentService.REGISTRATION_ERROR));
}
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
}
Exit 1: When the application in the visible (foreground) receives only one notification.

2: 2 .
