Skip to content

Service Worker Push requestSubscription error on SAFARI 12 #27889

Closed as not planned

Description

@robinComa

🐞 bug report

Affected Package

The issue is caused by package @angular/service-worker

Is this a regression?

I don't know

Description

The swPush requestSubscription break on SAFARI browser

🔬 Minimal Reproduction

import { Optional, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { SwPush } from '@angular/service-worker';
import { Observable } from 'rxjs';

import { environment } from '../../environments/environment';
import { PushNotification } from './notification';

@Injectable({
  providedIn: 'root'
})
export class NotificationService {

  constructor(
    private http: HttpClient,
    @Optional() private swPush: SwPush
  ) { }

  activate(): Observable<void> {
    return Observable.create((observer) => {
      this.swPush.requestSubscription({
        serverPublicKey: environment.webPush.publicKey
      }).then((subscription: PushSubscription) => {
        this.http.post(`${environment.serverUrl}/subscription`, subscription).subscribe(() => {
          observer.next();
          observer.complete();
        }, (err) => {
          observer.error(err);
        });
      }).catch((err) => {
        console.log(err): // => Here an error occurs on Safari : TypeError: undefined is not an object (evaluating 't.subscribe')
        observer.error(err);
      });
    });
  }

  sendNotification(notification: PushNotification): Observable<void> {
    return this.http.post<void>(`${environment.serverUrl}/notification`, notification);
  }

  messages(): Observable<PushNotification> {
    return <Observable<PushNotification>>this.swPush.messages;
  }
}

🔥 Exception or Error


TypeError: undefined is not an object (evaluating 't.subscribe')

🌍 Your Environment

Angular Version:


Angular CLI: 7.1.4
Node: 8.11.3
OS: darwin x64
Angular: 7.1.4
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router, service-worker

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.11.4
@angular-devkit/build-angular     0.11.4
@angular-devkit/build-optimizer   0.11.4
@angular-devkit/build-webpack     0.11.4
@angular-devkit/core              7.1.4
@angular-devkit/schematics        7.1.4
@angular/cdk                      7.2.0
@angular/material                 7.2.0
@angular/pwa                      0.11.4
@ngtools/webpack                  7.1.4
@schematics/angular               7.1.4
@schematics/update                0.11.4
rxjs                              6.3.3
typescript                        3.1.6
webpack                           4.23.1

AND : 
"@angular/service-worker": "~7.1.0",

Anything else relevant?
This issue is on Safari browser Version 12.0.2 (13606.3.4.1.4)

Activity

  1. added this to the needsTriage milestone on Jan 2, 2019
  2. gkalpak commented on Jan 3, 2019

    @gkalpak
    Member

    This is similar to #25781. It seems that on some browsers that otherwise support ServiceWorkers, ServiceWorkerRegistration#pushManager can be (under certain circumstances?) undefined.

    As a work-around, you could do one of the following:

    • Handle the error on this.swPush.subscription.
    • Check if pushManager is supported before trying to subscribe (e.g. 'PushManager' in window or 'pushManager' in ServiceWorkerRegistration.prototype or (await navigator.serviceWorker.getRegistration()).pushManager).

    Interestingly, MDN gives contradicting info on browser support, claiming that window.PushManager is not supported on Safari but ServiceWorkerRegistration#pushManager is supported on Safari 😕

    In any case, I think it makes sense to handle this more gracefully in @angular/service-worker (e.g. throw a more descriptive error if pushManager is not defined). Leaving this open as a feature request for that.

  3. robinComa commented on Jan 3, 2019

    @robinComa
    Author

    Hello @gkalpak thanks for quick reply !

    This workaround is to avoid the error on console, exact ? You mean that @ angular/service-worker cannot be used with the latest version of Safari? (this is the only way to do a PWA on IOS) 😞

  4. robinComa commented on Jan 3, 2019

    @robinComa
    Author

    Another thing : @angular/service-worker is not on the ng version result list

  5. gkalpak commented on Jan 3, 2019

    @gkalpak
    Member

    You mean that @ angular/service-worker cannot be used with the latest version of Safari?

    No, you can use @angular/service-worker, but if Safari does not (always?) support PushManager, then you can't use Push notifications on Safari. (You can still use the rest of the SW features.)

    Another thing : @angular/service-worker is not on the ng version result list

    This sounds like an issue for https://github.com/angular/angular-cli (although it may be intentional).

  6. robinComa commented on Jan 3, 2019

    @robinComa
    Author

    Ok I understand for the Safari case, do you know if Chrome on IOS will works for push notification in the future ?

    @angular/service-worker doesn't implement this kind of notification ?

    I create an issue on cli for the ng version "issue"

  7. gkalpak commented on Jan 3, 2019

    @gkalpak
    Member

    do you know if Chrome on IOS will works for push notification in the future ?

    I am pretty confident that both Chrome and Safari will fully support push notifications (at some point).

    @angular/service-worker doesn't implement this kind of notification ?

    No idea what this is (or even if it is different than regular notifications - the content seems outdated too).

  8. robinComa commented on Jan 3, 2019

    @robinComa
    Author

    Ok thanks for informations about support of push notification on IOS Chrome and Safari :-)
    This is the official page of Safari push notification (the last link was a demo that's work when you open the site with safari) :

  9. gkalpak commented on May 26, 2020

    @gkalpak
    Member

    I wonder if this is still an issue (it's been ~1.5year - maybe this has been fixed on latest Safari).

  10. 2 remaining items

  11. modified the milestones: needsTriage, Backlog on Oct 1, 2020
  12. zedL commented on Nov 7, 2020

    @zedL

    I also stated to use push notifications with Angular 10. "undefined is not an object (evaluating 't.getSubscription')" exeption is still there.

  13. zedL commented on Nov 7, 2020

    @zedL

    image

  14. yansokalau commented on Dec 30, 2020

    @yansokalau

    any update on this? does anyone have workarounds?

  15. cghislai commented on Apr 19, 2021

    @cghislai

    @yansokalau There are workarounds suggested above, but as far as I can tell, the bug is in the SwPush constructor, and injecting it in a service constructor in your app will trigger it. However, you an use angular Injector, and obtain an instance after checking it is supported by the browser. Something like

    constructor(private injector: Injector) {
      if (this.isSwPushSupported()) {
        this.swPush = this.injector.get(SwPush, undefined);
      }
    }
    
  16. genevpd commented on Jun 27, 2022

    @genevpd

    It would be great to have this fixed, especially now when Safari iOS 16 supports push notifications.

  17. hernays commented on Nov 2, 2022

    @hernays

    Hello my friends , not solutions in the topic?

    Hola amigos alguna solucion para Este tema me funciona Bien en todos Las plataformas Menos en safari - iphone

  18. twmobius commented on Jan 22, 2024

    @twmobius

    Are there any updates to this? It appears that this issue still exists on angular 16, which comes up on mobile Safari versions 16.6 and 17.2

  19. JeanMeche commented on Aug 29, 2025

    @JeanMeche
    Member

    With #63364 we've announced that the service-worker package in now in maintenance mode and will not receive feature PRs and feature requests.

    The message is also visible here: https://angular.dev/ecosystem/service-workers

  20. angular-automatic-lock-bot commented on Sep 29, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  21. locked and limited conversation to collaborators on Sep 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: service-workerIssues related to the @angular/service-worker packagebrowser: safaristate: confirmedtype: bug/fix

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions