ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.44k stars 977 forks source link

bug: iOS PushNotifications Register -- TO JS undefined #6673

Closed ullfindsmit closed 1 year ago

ullfindsmit commented 1 year ago

Bug Report

Capacitor Version

  @capacitor/cli: 5.0.4
  @capacitor/android: 5.0.4
  @capacitor/ios: 5.0.4
  @capacitor/core: 5.0.4

Platform(s)

iOS

Current Behavior

When calling the following code, get the message below and register event not fired.

await PushNotifications.register();

await PushNotifications.addListener('registration', (token) => {
  console.info('Registration token: ', token.value);
  //save token to db
  super.SaveToken2Server(token.value);
});

⚡️ [log] - Sat Jun 24 2023 00:10:43 GMT-0700 (Pacific Daylight Time) - Type: All - Message: native ⚡️ To Native -> PushNotifications checkPermissions 81213826 ⚡️ TO JS {"receive":"prompt"} ⚡️ To Native -> PushNotifications requestPermissions 81213827 ⚡️ TO JS {"receive":"granted"} ⚡️ To Native -> PushNotifications register 81213828 ⚡️ TO JS undefined

Expected Behavior

The registration even would be fired.

Code Reproduction


  async ngOnInit() {
    console.log("ngOnInit - app Component")
    this.listen();

    await PushNotifications.addListener('registration', (token) => {
      console.info('Registration token: ', token.value);
      //save token to db
      super.SaveToken2Server(token.value);
    });

  async requestPermission() {
    this.debug("Capacitor.isNativePlatform()=" + Capacitor.isNativePlatform());
    if (Capacitor.isNativePlatform()) {
      this.debug('native');
      // if it is native
      await this.registerNotifications();
    } else {
      Notification.requestPermission().then((permission) => {
        this.debug('Notification permission granted.', permission);
        if (permission === 'granted') {
          this.debug('Notification permission granted.');
          this.getToken();
        }
      });
    }
  }

  async registerNotifications() {
    let permStatus = await PushNotifications.checkPermissions();

    if (permStatus.receive === 'prompt') {
      permStatus = await PushNotifications.requestPermissions();
    }

    if (permStatus.receive !== 'granted') {
      throw new Error('User denied permissions!');
    }

    await PushNotifications.register();
  };

Other Technical Details

npm --version 9.5.1 node --version v18.16.0 pod --version 1.12.1

Additional Context

None

jcesarmobile commented 1 year ago

undefined is what the register method returns, that's not a bug

if you are not receiving the push notification token it's probably some misconfiguration in your app

Make sure you added push capabilities and this two methods in your AppDelegate.swift as explained in the docs

Also add the registrationError event listener as if it's failing to register, it will provide more information about what the problem is.

ionitron-bot[bot] commented 11 months ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.