2

同時に発生するいくつかのイベントがあります。ユーザーに複数の通知メッセージを連続して表示する必要があります。

理想的なケースは、約 2 秒間、各通知メッセージが順番に表示されることです。

私が得ることができる最も近いのは、複数のIDを使用することです。ただし、複数のIDを使用すると、望ましくない副作用があります。ステータスバーに同時に複数の通知メッセージが表示されます。

これにより、ユーザーのステータスが乱雑になりますが、これは私の意図ではありません。

複数の ID を使用する (複数の乱雑なメッセージが表示される)

int id = 0;
private void sendNotification(String message) {
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this.getActivity())
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle("MyApp")
            .setTicker(message)
            .setAutoCancel(true)
            .setOnlyAlertOnce(true)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentText(message);
    final NotificationManager mNotificationManager =
            (NotificationManager) this.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(id++, mBuilder.build());   
}

ただし、複数の ID を使用せずに単一の ID を使用すると、複数の通知要求が同時に行われた場合に 1 つのメッセージのみが表示されます。すべてのメッセージが順番に表示されます。メッセージが失われることはありません。

4

2 に答える 2

3

ステータス バーに1 つだけ必要であるNotificationが、アプリのすべての通知をメッセージで示したい場合は、メッセージをNotification変更して 1 つを管理する必要があります。

Notificationwithを再投稿するたびsendNotification(message)に、Notification tickerテキストが更新されます。

たとえば、架空の受信トレイにアイテムの通知が 1 つある場合、次のようなメッセージを投稿できます。

ジョーからのメッセージ。

複数の新しいメッセージを受信した場合は、次のように変更します。

(n) 件のメッセージがあります。

ユーザーが を押したとき、すべてのメッセージを表示するには、またはのようなものですべてを表示する にNotificationリダイレクトする必要があります。ActivityListViewGridView

于 2013-04-11T15:55:39.323 に答える
1

独自のメッセージ キューイング システムを作成します。単一のIDを使用していませんが、私が望むものを達成できます

複数の通知要求が同時に行われた場合、1 つのメッセージのみが表示されます。すべてのメッセージが順番に表示されます。メッセージが失われることはありません。

this.blockingQueue.offer(message);

完全なメッセージ キューイング システムを次に示します。

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    executor.submit(new NotificationTask());
}

private class NotificationTask implements Runnable {
    @Override
    public void run() {
        while (!executor.isShutdown()) {
            try {
                String message = blockingQueue.take();
                notification(message);
                // Allow 5 seconds for every message.
                Thread.sleep(Constants.NOTIFICATION_MESSAGE_LIFE_TIME);
            } catch (InterruptedException ex) {
                Log.e(TAG, "", ex);
                // Error occurs. Stop immediately.
                break;
            }                
        }

    }
}

@Override
public void onDestroy() {
    // Will be triggered during back button pressed.
    super.onDestroy();
    executor.shutdownNow();
    try {
        executor.awaitTermination(100, TimeUnit.DAYS);
    } catch (InterruptedException ex) {
        Log.e(TAG, "", ex);
    }
}

private void notification(String message) {
    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this.getActivity())
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle(this.getString(R.string.app_name))
        .setTicker(message)
        .setContentText(message)
        .setAutoCancel(true)
        .setOnlyAlertOnce(true);

    // Do we need to have sound effect?

    final NotificationManager mNotificationManager =
        (NotificationManager) this.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
    final int id = atomicInteger.getAndIncrement();
    mNotificationManager.notify(id, mBuilder.build());

    Handler handler = new Handler(Looper.getMainLooper());
    handler.postDelayed(new NotificationManagerCancelRunnable(mNotificationManager, id), Constants.NOTIFICATION_MESSAGE_LIFE_TIME);
}

// Use static class, to avoid memory leakage.
private static class NotificationManagerCancelRunnable implements Runnable {
    private final NotificationManager notificationManager;
    private final int id;

    public NotificationManagerCancelRunnable(NotificationManager notificationManager, int id) {
        this.notificationManager = notificationManager;
        this.id = id;
    }

    @Override
    public void run() {
        notificationManager.cancel(id);
    }
}

// For notification usage. 128 is just a magic number.
private final ExecutorService executor = Executors.newSingleThreadExecutor();
private final BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<String>(128);
private final AtomicInteger atomicInteger = new AtomicInteger(0);    
于 2013-04-12T03:21:24.293 に答える