1

i'm trying show a notification in my app... i declared a sample text as title and body to check whether it is running or not. it was perfect. then, when i changed the String values(title,body). it is not updated, it still shows old sample text. i have searched for solutions on the internet. some said adding a flag_update_current would solve. i did, but no use. here is the code.. i used.

public void Notify(){
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    String mbody = "Mode: General";
    String title = "messager On";
    Notification n = new Notification(R.drawable.ezmsgr,mbody,System.currentTimeMillis());
    n.setLatestEventInfo(this, title, mbody, pi);
    n.defaults = Notification.DEFAULT_LIGHTS;
    n.flags = Notification.FLAG_ONGOING_EVENT;
    nm1.notify(NID,n);
}

i tried assinging new values to unique id of notification too. it is not updating.

4

1 回答 1

0

一些东西:

  1. notify()每次要更新通知的外观时,您都需要再次调用。

  2. 您正在使用一堆已弃用的 API 来构建您的通知。相反,您想使用Notification.Builder,如:

Notification n = new Notification.Builder(this)
        .setSmallIcon(R.drawable.ezmsgr)
        .setContentTitle(title)
        .setContentText(mbody)
        .setContentIntent(pi)
        .setDefaults(Notification.DEFAULT_LIGHTS)
        .setOngoing(true) // ugh, why are you doing this?
        .build();
nm.notify(NID, n);

notify()每次要更新文本时再次调用。您可以重复使用相同的构建器或创建一个新构建器。

于 2013-05-25T17:55:47.670 回答