如何以正确的方式更新通知的内容?

时间:2015-01-09 09:08:39

标签: android android-notifications

我在更新通知内容时遇到问题,但未在通知窗口中重新排列。

官方文档说,只要notification IDTAG相同,通知就会被替换。这正是发生的事情,除了一个问题 - 每次我更新它时都会被推到列表顶部(除非优先级设置为LOWMIN,我不想要。)

通过不使用setDefaults()在构建器上设置任何默认值来发布静默通知可以解决问题,但这又不是必需的。

我做错了什么?我知道这是可能的,因为Gmail应用可以将其通知更改为"撤消"没有重新安排自己。

以下是我使用的代码:

void showFirstNotif(){

    mNotifBuilder.setContentTitle("First")
            .setContentText("Body")
            .setWhen(activityStartTime)
            .setSmallIcon(PhotoUtils.getAppIconResId())
            .setDefaults(Notification.DEFAULT_ALL)
            .setOnlyAlertOnce(true);

    NotificationManagerCompat.from(this).notify("Test", NOTIF_ID, mNotifBuilder.build());

}

void showSecondNotif(){

    mNotifBuilder.setContentTitle("Second")
            .setContentText("Body")
            .setWhen(activityStartTime)
            .setSmallIcon(PhotoUtils.getAppIconResId())
            .setDefaults(Notification.DEFAULT_ALL)
            .setOnlyAlertOnce(true);

    NotificationManagerCompat.from(this).notify("Test", NOTIF_ID, mNotifBuilder.build());

}

1 个答案:

答案 0 :(得分:1)

我猜你的情况是Android正在重建视图。您只需拨打一条消息即可致电updateNotification(String),它会更新通知,而不会让用户烦恼并重新安排Notification

我创造了一个例子,它的工作非常好。看看:

public class MainActivity extends Activity {

    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    private int mNotificationId1 = 1;
    private int mNotificationId2 = 2;
    private int mNotificationId3 = 3;
    private int mNotificationId4 = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(MainActivity.this);

        mBuilder.setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Notification")
                .setContentText("Notification No 1");
        mNotifyManager.notify(mNotificationId1, mBuilder.build());

        mBuilder.setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Notification")
                .setContentText("Notification No 2");
        mNotifyManager.notify(mNotificationId2, mBuilder.build());

        mBuilder.setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Notification")
                .setContentText("Notification No 3");
        mNotifyManager.notify(mNotificationId3, mBuilder.build());

        mBuilder.setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Notification")
                .setContentText("Notification No 4");
        mNotifyManager.notify(mNotificationId4, mBuilder.build());

        findViewById(R.id.button).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                updateNotification("Notification 2 changed silently");
            }
        });
    }

    private void updateNotification(String message) {
        mBuilder.setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Notification")
                .setDefaults(Notification.DEFAULT_ALL);
        mBuilder.setContentText(message);
        mNotifyManager.notify(mNotificationId2, mBuilder.build());
    }
}

但是如果您使用:

private void updateNotification(String message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(
                MainActivity.this);
        builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle(
                "Notification").setDefaults(Notification.DEFAULT_ALL);
        builder.setContentText(message);
        mNotifyManager.notify(mNotificationId2, builder.build());
    }

在这种情况下,虽然您使用相同的通知ID,但它已经重建,因此被推到顶部,好像它本身就是一个新的Notification