1

I am creating a notification and when it is received the app is launched. My question is how do i stop that from happening?

final private static void generateNotification(Context context, String message){
    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);


    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(context.getString(R.string.app_name))
        .setContentIntent(intent)
        .setContentText(message)
        .setAutoCancel(true) 
        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, mBuilder.build());
} 

The notification is called in GCMIntentService:

@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String response = intent.getExtras().getString("response");

    switch(response){
        case "logout":
            String message = "You logged into another device!";
            //displayMessage(context, message);
            // notifies user
            generateNotification(context, message);
            Intent i = new Intent(context, MainActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
        break;
        default:

        break;
    }



}
jampez77
  • 5,012
  • 7
  • 32
  • 52
  • "when it is received the app is launched", you sure? not when clicked? – weston May 20 '15 at 14:44
  • yes, even if i don't click on the notification the app is launched – jampez77 May 20 '15 at 14:45
  • Post where is this code called from, as I don't think problem is in here. – weston May 20 '15 at 14:47
  • Do you have a service and a broadcast receiver firing over and over? If so that's the problem – Xjasz May 20 '15 at 14:49
  • @Jasz I don't have a broadcast reciever :S – jampez77 May 20 '15 at 14:52
  • Maybe I'm wrong, but don't you think it is just because you start an Intent just after displaying your notification ? `generateNotification(context, message); Intent i = new Intent(context, MainActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(i);` Your notification is displaying here right ? But you call a new Intent of your MainActivity then, which open your app. – Kapcash May 20 '15 at 14:54

1 Answers1

1

Just don't do these lines which I have commented out below. That code starts an activity on the "logout" message received.

generateNotification(context, message);
//Intent i = new Intent(context, MainActivity.class);
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//startActivity(i);
weston
  • 54,145
  • 21
  • 145
  • 203
  • Excellent! That works, however now how do I run that on notification tap or if the app is open? – jampez77 May 20 '15 at 14:58
  • Have you tried tapping it? As your code seems to have that though use of `setContentIntent` – weston May 20 '15 at 15:00
  • As for is app open: http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not – weston May 20 '15 at 15:00