android - 关闭 Android 推送通知

标签 android push-notification push dismiss

关于推送通知的一些问题。当我按下其中一个通知时,如何关闭我的应用程序的所有通知?另外...如果我启动应用程序(不是来自通知),我该如何关闭通知?

这是我从 GcmIntentService 类发送通知的方式(如果有帮助的话)。

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());
}

最佳答案

我不确定您发出的通知是否针对同一事件,如果是这样,您可以像 Gmail 一样在收到第二封或第三封电子邮件时执行所谓的“堆叠通知”。这一切都在一个通知中,因此将作为一个通知消失。这仅兼容 4.1 以上版本。

这是 Google 随堆叠一起提供的代码。这有什么帮助吗?

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()); 

关于android - 关闭 Android 推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21695614/

相关文章:

java - Android 在 Activity 和不活动时通过 Java 更改 EditText 重音的颜色

android - 附上缺少的源android

不使用服务器的 iOS 推送通知

javascript - 如何规避 float :left; behavior in a masonry layout

android - 我在任何 Android 模板中都找不到 Main.axml,也无法打开其他 axml 文件

android - 你能检测到 Android 应用程序何时进入后台吗?

mercurial - 如何在推送默认分支时阻止命名分支?

git - 递归 Git 推/pull ?

java - 从 SD 卡中删除文件夹

ios - APS 缺少权利