These two methods defined by the Push API are used to get the push subscription from the browser. What is the difference between them?
The answer is simple:
- PushManager.subscribe() creates a new subscription or returns the current subscription if it already exists (note that it never changes the current subscription if it already exists, so it's safe to call it multiple times)
- PushManager.getSubscription() returns the existing subscription if it exists, otherwise returns
null
.
Note that PushManager.subscribe()
may trigger a permission prompt, if the user has not allowed the notifications yet, while PushManager.getSubscription()
will never trigger it.
If you use Pushpad for web push notifications you don't need to call these methods directly: you can use the JavaScript SDK instead, which offers useful methods and an higher level of abstraction.