Some question about push notifications. How do I dissmis all the notifications of my app when I press one of them? Also ... how do I dissmiss the noifications if I start the app (not from the noification)?

Here is how I send a notification from my GcmIntentService class (if it is helpfull).

private void sendNotification(Bundle extras) {
    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    NOTIFICATION_ID = Integer.parseInt(extras.getString("id"));

    Intent in = new Intent(this, PushActivity.class);
    in.putExtras(extras);
    in.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    String msg = extras.getString("msj");

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            in, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentTitle("MY TITLE")
    .setAutoCancel(true)
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(msg))
    .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
有帮助吗?

解决方案

I'm not sure if the notifications you are putting out are for the same event, if so you can do what is called "stacking notifications" like Gmail does when you get a second or third email. It's all in the one notification and therefore will go away as one. This is only compatible above 4.1.

This is the code Google gives along with the stacking. This any help?

mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText)
    .setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
        notifyID,
        mNotifyBuilder.build()); 

其他提示

try android.app.NotificationManager.cancelAll()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top