0

I have tested the intent of notification about 4 or 5 days. I diappointed myself and I can't reach my goal. I think it's not too difficult but I can't get it. I want to go current running activity when I click the notification. I used many things in intent.

   notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
   notificationIntent.setAction(Intent.ACTION_MAIN);
   notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);

In pendingIntent,

     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

In manifest,

    android:launchMode="singleTop" // android:launchMode="singleTask" // android:launchMode="singleInstance"

but it always go the class I give at intent.Somebody help me, I have no idea.

Halo
  • 729
  • 1
  • 8
  • 18
  • Maybe this SO [link](http://stackoverflow.com/questions/20896615/push-notifications-service-for-android-4-4/20995515#20995515) might help you. – ChuongPham Apr 05 '14 at 07:48
  • @Halo what your current activity name and which activity hold by notificationIntent? – Hardik Apr 05 '14 at 07:58
  • MainActivity is current activity and Goo.class is at notificationIntent. – Halo Apr 05 '14 at 08:01
  • ok then it is not possible you should open mainActivity(so your intent should point MainActivity instead of Goo class) and override onNewIntent() method in mainactivity and open Goo.class from it. – Hardik Apr 05 '14 at 08:04
  • @Halo i edited my answer see here http://stackoverflow.com/questions/22834790/notification-cant-go-to-current-running-activity/22837735#22837735 – Hardik Apr 05 '14 at 08:10

1 Answers1

0

Try something like:

@SuppressLint("NewApi")
public static void showNotification(Context context, String message) {
    Intent intent = new Intent(context, MainActivity.class);
    // Ensures navigating back to activity, then to Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Add the activity parent chain to the stack builder.
    stackBuilder.addParentStack(MainActivity.class);
    // Add a new Intent to the task stack.
    stackBuilder.addNextIntent(intent);     
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Create notification.
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        // API 16 onwards.
        Notification.Builder builder = new Notification.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_notifier)
            .setTicker(message)
            .setContentText(message)
            .setPriority(Notification.PRIORITY_HIGH)
            .setStyle(new Notification.BigTextStyle().bigText(message)) 
            .setWhen(System.currentTimeMillis());
            mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());         
    } else {
        // API 15 and earlier.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_notifier)
            .setTicker(message)
            .setContentText(message)
            .setPriority(Notification.PRIORITY_HIGH)
            .setWhen(System.currentTimeMillis());
        mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());         
    }
}
ChuongPham
  • 4,761
  • 8
  • 43
  • 53
  • 2
    You don't have to tick my answer right away. Only tick it if it works for you so others who read your post in the future know that it work and they can use my answer too! – ChuongPham Apr 05 '14 at 08:19
  • Give my respest but will it ok when it use in service. – Halo Apr 05 '14 at 08:25
  • I tend to use notification in a BOOT_COMPLETED event, or when I'm processing information received from a BroadcastReceiver via an Intent. I have not come across a situation where I needed to use notification in a Service - but I guess it will work. You may need to do more testing in the event that your service is killed by Android if it's low on memory. So, what will happen to your notification in this case. – ChuongPham Apr 05 '14 at 08:32
  • May be stackBuilder was called somewhere? I don't know stackBuilder is what for. – Halo Apr 05 '14 at 11:43
  • More info regarding `TaskStackBuilder` can be found [here](http://developer.android.com/reference/android/support/v4/app/TaskStackBuilder.html). In short, it keeps a back stack or history of your new task. – ChuongPham Apr 05 '14 at 11:56
  • Now I have a problem, If app is killed and notification is get, I clicked on notification and it didn't go anywhere.. – Halo Apr 06 '14 at 03:48
  • Yoosha!!! I got it!!! I added intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); I got what I want. Thank you, brother. – Halo Apr 06 '14 at 04:01
  • Yes, that's how you lauched an intent. Normally, in a BroadcastReceiver situation, you wouldn't need it. Anyway, I'm glad you solve this little issue yourself. Good work! – ChuongPham Apr 06 '14 at 04:15