1

i am create messenger with android, every message receive will show notification, when app receive some message from some one will call this method, content is message from friend, and title is name of friend, and every people have ID, and also i pass Friend ID to FriendID parameter.

public static void CreateNotify(Context context, String Content, String title, String FriendID) {
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
    .setSmallIcon(R.drawable.ic_launcher)
    .setLargeIcon(icon)
    .setContentTitle(title)
    .setContentText(Content)
    .setLights(Color.BLUE, 1000, 500)
    .setAutoCancel(true);

    Intent resultIntent = new Intent(context, Messenger.class);
    resultIntent.putExtra("ID", FriendID);
int mNotificationId = FriendID.hashCode();
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, mNotificationId, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT|Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

    mBuilder.setContentIntent(resultPendingIntent);

    NotificationManager mNotifyMgr = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

every things is ok, but if my friend (A) send me message i see notification and i click it will show me Messenger activity, in this state my friend (B) send me message i click on notification i will see messanger of my (B) friend... now when my friend (A) send me new text message, in notification will show me notification from my (A) and when i click on it Messenger activity will create new and will show me... i want instead of create new instance, bring to front last instance of messenger (A)

Emran Sadeghi
  • 612
  • 6
  • 20
  • 1
    Small tips: by Java convention method name should start with a lowercase letter. By Android convention prefix 'm' should be added only to an instance variable name. – makovkastar Apr 11 '14 at 12:41
  • yes i am C# programmer, and it is very hard for me to use java tips, however thanks to tell me. – Emran Sadeghi Apr 11 '14 at 12:57

1 Answers1

1

Some good answers are found in this question. In short, a good way is to add to your manifest

<activity 
...
android:launchMode="singleTask" >

which ensures that only a single copy of the activity is running, and brings that to the front. Depending on your goal, you might also want to use some of the flags available under Intent.

Community
  • 1
  • 1
Tad
  • 4,668
  • 34
  • 35