capacitor: PushNotifications.requestPermissions is not a function

Bug Report

Using @capacitor/ios 2.0.0 and trying to ask for push notification permission, an error appear in XCode console

` ⚡️ [log] - Initializing HomePage ⚡️ [error] - ERROR TypeError: PushNotifications.requestPermissions is not a function. (In ‘PushNotifications.requestPermissions()’, ‘PushNotifications.requestPermissions’ is undefined) ⚡️ [error] - ERROR CONTEXT [object Object]

`

Capacitor Version

npx cap doctor output:

`Latest Dependencies:

@capacitor/cli: 2.0.0

@capacitor/core: 2.0.0

@capacitor/android: 2.0.0

@capacitor/ios: 2.0.0

Installed Dependencies:

@capacitor/cli 1.5.1

@capacitor/android 1.5.1

@capacitor/ios 2.0.0

@capacitor/core 1.5.1

[success] Android looking great! 👌 Found 18 Capacitor plugins for ios: call-number (1.0.1) com.telerik.plugins.nativepagetransitions (0.6.5) cordova-plugin-actionsheet (2.3.3) cordova-plugin-camera (4.0.3) cordova-plugin-device (2.0.2) cordova-plugin-dialogs (2.0.1) cordova-plugin-geolocation (4.0.1) cordova-plugin-inappbrowser (3.0.0) cordova-plugin-ionic-keyboard (2.1.3) cordova-plugin-ionic-webview (4.0.1) cordova-plugin-screen-orientation (3.0.1) cordova-plugin-splashscreen (5.0.2) cordova-plugin-statusbar (2.4.2) cordova-plugin-whitelist (1.3.3) cordova-plugin-x-socialsharing (5.4.4) es6-promise-plugin (4.2.2) mx.ferreyra.callnumber (0.0.2) uk.co.workingedge.phonegap.plugin.launchnavigator (5.0.4) [success] iOS looking great! `

Affected Platform(s)

  • Android
  • iOS
  • Electron
  • Web

Current Behavior

iOS is not asking for push notifications permissions

Expected Behavior

iOS should ask for push notifications permissions

Sample Code or Sample Application Repo

private enablePush(): void {
        PushNotifications.requestPermissions().then( (result: any) => {
            if (result.granted) {
                PushNotifications.register().then((val) => {
                    alert('r: ' + JSON.stringify(val));
                }).catch(e => {
                    alert('r: ' + JSON.stringify(e));
                });
            }
        });

        PushNotifications.addListener('registration',
            (token: PushNotificationToken) => {
                alert('Push registration success, token: ' + token.value);
            }
        );

        PushNotifications.addListener('registrationError',
            (error: any) => {
                alert('Error on registration: ' + JSON.stringify(error));
            }
        );

        PushNotifications.addListener('pushNotificationReceived',
            (notification: PushNotification) => {
                alert('Push received: ' + JSON.stringify(notification));
            }
        );
        PushNotifications.addListener('pushNotificationActionPerformed',
            (notification: PushNotificationActionPerformed) => {
                alert('Push action performed: ' + JSON.stringify(notification));
            }
        );
    }

Reproduction Steps

  • Create a blank capacitor project
  • Follow this tutorial for push notification with FCM https://capacitor.ionicframework.com/docs/guides/push-notifications-firebase/
  • and run in xcode

Other Technical Details

pod --version: 1.9.1

Other Information

This’s my first capacitor project

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (5 by maintainers)

Most upvoted comments

Seriously, I am disgusted by these instabilities between versions. What then is the annotation @ Deprecated if the functions must disappear without warning.

I think it is a typing issue. Followed worked for me.

(PushNotifications as any).requestPermission().then(result => { PushNotifications.register(); });

@ThonyFD that is not your bad, the documentation isn’t accurate and waste of hours. There are tons of plugins I used with capacitor and all has documentation accuracy issues between the versions…

The default installation comes with the stable version and the documentation doesn’t cover current stable version. Still I couldn’t figure out how to use PushNotifications.requestPermissions with my current installation. I am not going to update the capacitor as after weeks, I already have mature application and can’t risk it with upgrading capacitor.

So, how can I use PushNotifications.requestPermissions ? How to fix PushNotifications.requestPermissions is not a function error?

It’s requestPermission, without s