In my application, I have a push notification integrated with GCM . His work is beautiful whenever a notification appears. But a push notification appears even when the user is inside the application. I want to see notifications only if the user is outside the application.
Here is my push notification code:
GcmBroadcastReceiver
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { ComponentName comp = new ComponentName(GCMNotificationIntentService.class.getPackage().getName(), GCMNotificationIntentService.class.getName()); startWakefulService(context, (intent.setComponent(comp))); setResultCode(Activity.RESULT_OK); } }
GCMnotificationIntentService.Class
public class GCMNotificationIntentService extends IntentService { public static int notificationId = 10; private NotificationManager mNotificationManager; NotificationCompat.Builder builder; private boolean login_status = false; public GCMNotificationIntentService() { super("GcmIntentService"); } public static final String TAG = GCMNotificationIntentService.class.getSimpleName(); @Override protected void onHandleIntent(Intent intent) { Bundle extras = intent.getExtras(); GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
In ManifestFile
<receiver android:name=".GcmBroadcastReceiver" 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.medlife.deliveryagent" /> </intent-filter> </receiver> <service android:name=".GCMNotificationIntentService" />
Any suggestions would be highly appreciated.
source share