2

次のコードを使用して通知を表示します

private void SendNotification(String notificationTitle, String notificationMessage) 
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, notificationMessage,
        System.currentTimeMillis());

Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("notif_id", "5100");

//Setting the single top property of the notification and intent.
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify((int)System.currentTimeMillis(), notification);
}

新しい通知が来るたびに、前の通知は削除され、新しい通知が表示されます。

どんな手がかりも役に立ちます。

事前にサンクス

o.

4

2 に答える 2

4

毎回異なる ID (System.currentTimeMillis()) で通知を生成しています。単一の ID に変更してください。

notificationManager.notify(0, notification);

ステータスバーに表示される通知を投稿します。同じ ID の通知がアプリケーションによって既に投稿されていて、まだキャンセルされていない場合は、更新された情報に置き換えられます。

NotificationManager.html

于 2013-06-04T11:15:04.067 に答える