# Push notification time window mins can be double that time

**URL:** https://meta.discourse.org/t/push-notification-time-window-mins-can-be-double-that-time/259688
**Category:** Bug
**Created:** [March 28, 2023, 12:48pm UTC](https://meta.discourse.org/t/push-notification-time-window-mins-can-be-double-that-time/259688 "2023-03-28T12:48:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![RGJ](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rgj/32/523185_2.png) [@RGJ](https://meta.discourse.org/u/RGJ)
#### Post date: [March 28, 2023, 12:48pm UTC](https://meta.discourse.org/t/push-notification-time-window-mins-can-be-double-that-time/259688/1 "2023-03-28T12:48:29Z")

</div>

Since we now FINALLY have push notifications on iOS PWA 🎉 I was looking into the delay that is applied before a push notification is being sent - because it seemed to take longer than expected.

The [code](https://github.com/discourse/discourse/blame/e636abeb0d92855cd97742a5d48d7018284106ee/app/services/post_alerter.rb#L57-L66) checks if a user was online recently (less than `push_notification_time_window_mins` ago) and if they were then the notification is delayed for `push_notification_time_window_mins`

```plaintext
      if user.seen_since?(SiteSetting.push_notification_time_window_mins.minutes.ago)
        Jobs.enqueue_in(
          SiteSetting.push_notification_time_window_mins.minutes,
          ...
        )
      else
        Jobs.enqueue(:send_push_notification, user_id: user.id, payload: payload)
      end

```

But this means that in practice this can take up to as twice as long as expected since the `push_notification_time_window_mins` is applied both in the online check as in the delay.

Example:

`push_notification_time_window_mins` is set to 10

**13:00**  
I leave the site  
**13:10**  
Something happens that triggers a notification.  
Code checks whether I was online less than 10 minutes ago.  
I was, so the notification will be sent at **13:20**.

**12:59**  
I leave the site  
**13:10**  
Something happens that triggers a notification.  
Code checks whether I was online less than 10 minutes ago.  
I was not, so the notification will be sent immediately, at **13:10**.

So one minute difference in my online time can change the notification delivery with almost `push_notification_time_window_mins`. Especially if the setting is changed into a higher value this can make a significant difference.

I would expect the delay to be push\_notification\_time\_window\_mins - (now - seen\_since)

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [March 28, 2023, 3:55pm UTC](https://meta.discourse.org/t/push-notification-time-window-mins-can-be-double-that-time/259688/3 "2023-03-28T15:55:41Z")

</div>

This should fix it

[https://github.com/discourse/discourse/pull/20864](https://github.com/discourse/discourse/pull/20864)

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [April 1, 2023, 11:00am UTC](https://meta.discourse.org/t/push-notification-time-window-mins-can-be-double-that-time/259688/4 "2023-04-01T11:00:59Z")

</div>

This topic was automatically closed after 3 days. New replies are no longer allowed.
