简体   繁体   中英

Android Notification use PendingIntent not new one

I have implemented some notifications in my application. It seems that everytime I click on the notification it starts a new intent. The goal is to bring the application in the foreground because it is still in background (so not killed).

How can I achieve this?

mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, ChatActivity.class), 0);
    long[] vibration = new long[]{100, 250}; 

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.icon)
    .setAutoCancel(true)
    .setTicker(msg)
    .setLights(Color.WHITE, 300, 600)
    .setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.new_msg))
    .setVibrate(vibration)
    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

To bring the application to the front, use the following intent in the Pending Intent.

Intent intent = new Intent(this, ChatActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

This'll resume the application if not already killed or open the ChatActivity if it has been killed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM