Capacitor: PushNotifications.requestPermissions is not a function

Created on 4 Apr 2020  路  15Comments  路  Source: ionic-team/capacitor

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
  • [X] 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

Other Technical Details

pod --version: 1.9.1

Other Information

This's my first capacitor project

Most helpful comment

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

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

All 15 comments

It鈥檚 requestPermission, without s

@jcesarmobile Something it's really wrong with this package, this method does not exist without s,

Captura de Pantalla 2020-04-04 a la(s) 11 02 34 a 聽m

My bad @jcesarmobile, I only update capacitor-ios to 2, after update all capacitor to 2.0.0, all works as expected, thank's for the feedback

@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?

Docs are for 2.x, they are not versioned.
2.x is the stable version.

I created my app 2 months ago and capacitor was setup with v1.4

And just after two months, its lots of functions and plugin compatibilities are changed and can鈥檛 find documentation for that

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

The problem is still current with 2.2.0.

With requestPermissions working in web, but on IOS : requestPermissions is not a function

I think some of the confusion comes from this:

The PushNotificationsPlugin contains requestPermission for requesting push notifications permissions:

https://github.com/ionic-team/capacitor/blob/26e5e7415a1b61e374997d9a4a18003746fa0704/core/src/core-plugin-definitions.ts#L1615

But then there's a requestPermissions on the Plugin interface it extends, which all plugins have:

https://github.com/ionic-team/capacitor/blob/26e5e7415a1b61e374997d9a4a18003746fa0704/core/src/definitions.ts#L5

I can't discern why the push notifications plugin adds a new method.

I also have the problem with the Camera plugin

Today, I recommand #Flutter and I think that It's the future with #Google in background. Still the same goal : One Code for the both.

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

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

I am still stuggling.
I can't able to find the answer
error is PushNotifications.requestPermissions is not a function

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

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

This worked, thanks.

Make sure to update all capacitor related packages to the latest version.
@capacitor/ios
@capacitor/cli
@capacitor/core
@capacitor/android -> Only if you are developing for Android as well.

Was this page helpful?
0 / 5 - 0 ratings