ionic-team / capacitor

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

PushNotifications.requestPermissions is not a function #2688

Closed ThonyFD closed 4 years ago

ThonyFD commented 4 years ago

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)

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

jcesarmobile commented 4 years ago

It’s requestPermission, without s

ThonyFD commented 4 years ago

@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
ThonyFD commented 4 years ago

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

yigitbacakoglu commented 4 years ago

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

jcesarmobile commented 4 years ago

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

yigitbacakoglu commented 4 years ago

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’t find documentation for that

elgsylvain85 commented 4 years ago

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

KyDenZ commented 4 years ago

The problem is still current with 2.2.0.

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

imhoffd commented 4 years ago

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.

KyDenZ commented 4 years ago

I also have the problem with the Camera plugin

elgsylvain85 commented 4 years ago

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.

hgrewa commented 4 years ago

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

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

rathorrajnesh commented 4 years ago

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

lbarbas commented 4 years ago

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

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

This worked, thanks.

adityasomann commented 4 years ago

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.

Kaoschuks commented 3 years ago

just use

PushNotifications.register();

not

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

worked for me

xmarkclx commented 3 years ago

Use PushNotifications.requestPermission() with no s.

You need to do this for iOS pns to work.

ionitron-bot[bot] commented 1 year 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.