Generate int unique id as android notification id

后端 未结 7 557
难免孤独
难免孤独 2020-12-05 16:37

When I send multiple push notifications, I need them to be all shown in the notification bar ordered by the time sent desc. I know I should use unique notification - I tried

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 17:23

    Maybe not the best, but definitely the simplest is to use current time.

    int oneTimeID = (int) SystemClock.uptimeMillis();
    mNotificationManager.notify(oneTimeID, mBuilder.build());
    

    The good: this is the easiest way to get increasing ids.

    The bad: time is a long and we're truncating it to half of that. This means that the counter will wrap around every 2'147'483'647 /1000(ms->s)/60(s->m)/60(m->h)/24(h->d) =~25 days.

    SystemClock.uptimeMillis() has 2 advantages over currentTimeMillis:

    1. discounts all the milliseconds spent at deep sleep, what decreases amount of wraparounds.
    2. starts at 0 when the phone is restarted.

提交回复
热议问题