Capacitor: bug: PushNotifications is missing the removeAllListeners function

Created on 19 Apr 2020  路  14Comments  路  Source: ionic-team/capacitor

Bug Report

Capacitor Version

npx cap doctor output:

Installed Dependencies:

  @capacitor/core 1.5.1

  @capacitor/cli 1.5.1

  @capacitor/android 1.5.2

  @capacitor/ios 1.5.2


[success] Android looking great! 馃憣
  Found 4 Capacitor plugins for ios:
    capacitor-fcm (2.0.0)
    cordova-plugin-add-swift-support (1.7.2)
    cordova-plugin-file (6.0.2)
    cordova-plugin-photo-library (2.2.1)
[success] iOS looking great! 馃憣

Affected Platform(s)

  • [x] Android
  • [x] iOS
  • [x] Electron
  • [x] Web

Current Behavior

  1. PushNotifications.removeAllListeners is not a function.

  2. PushNotifications.requestPermissions returns an empty object.

Expected Behavior

  1. Removes all listeners.

  2. Returns an object with granted property.

Sample Code or Sample Application Repo

PushNotifications.removeAllListeners();

Other Technical Details

When I do console.log(Object.keys(PushNotifications)), I get only:

[
"addListener",
"listChannels",
"removeDeliveredNotifications",
"createChannel",
"getDeliveredNotifications",
"requestPermissions",
"removeAllDeliveredNotifications",
"deleteChannel",
"register"
]

But in the docs, there is a removeAllListeners method?

npm --version output:

6.13.1

node --version output:

v13.2.0

pod --version output (iOS issues only):

1.8.4

Most helpful comment

In capacitor 1 calling register also requested the permission, but doesn't do it in Capacitor 2.

But please, don't keep asking questions on github issues.

For questions you can ask on the forum, slack or stack overflow using capacitor tag.

All 14 comments

Also, I don't want to update to latest version of capacitor because of old cordova plugin issues. Thank you.

After hours hours hours,
I just updated to latest version of capacitor and it still doesn't work. ( PushNotifications.requestPermission )
If you directly use PushNotifications.register(), it shows prompt but not gets token and not fires up registration eventListeners ...

Update:

I resync the cap with ios 'npx cap sync ios' and now PushNotifications.requestPermission prompts request alert but registration event listener still can't receive the token .

After hours hours hours,
I just updated to latest version of capacitor and it still doesn't work. ( PushNotifications.requestPermission )
If you directly use PushNotifications.register(), it shows prompt but not gets token and not fires up registration eventListeners ...

Update:

I resync the cap with ios 'npx cap sync ios' and now PushNotifications.requestPermission prompts request alert but registration event listener still can't receive the token .

Are you testing it on a real device or simulator?

After hours hours hours,
I just updated to latest version of capacitor and it still doesn't work. ( PushNotifications.requestPermission )
If you directly use PushNotifications.register(), it shows prompt but not gets token and not fires up registration eventListeners ...
Update:
I resync the cap with ios 'npx cap sync ios' and now PushNotifications.requestPermission prompts request alert but registration event listener still can't receive the token .

Are you testing it on a real device or simulator?

I am testing on a real device.

On the otherhand, same codebase works for adroid.

I rechecked the IOS setup again and again and followed several tutorials including the official one, and I鈥檓 pretty sure that all certificates, setup codes are in its place.

What would be the reason of not getting token after granting permissions?

@yigitbacakoglu After register your device with Push notification.

In ios, we only receive the APNS token, not FCM token. So, after registering your device, try to use this plugin to get FCM token (https://github.com/stewwan/capacitor-fcm)

fcm.getToken().then(tObj => { if (tObj && tObj.token) { const fcmToken = tObj.token; } });

After hours hours hours,
I just updated to latest version of capacitor and it still doesn't work. ( PushNotifications.requestPermission )
If you directly use PushNotifications.register(), it shows prompt but not gets token and not fires up registration eventListeners ...
Update:
I resync the cap with ios 'npx cap sync ios' and now PushNotifications.requestPermission prompts request alert but registration event listener still can't receive the token .

Are you testing it on a real device or simulator?

I am testing on a real device.

On the otherhand, same codebase works for adroid.

I rechecked the IOS setup again and again and followed several tutorials including the official one, and I鈥檓 pretty sure that all certificates, setup codes are in its place.

What would be the reason of not getting token after granting permissions?

Me too. The setup was working on Android and not on IOS. No errors at all.

I'm using ionic and react and here's my code:

  useEffect(() => {
    if (!state.isAuth) {
      return;
    }

    const registerToken = async () => {
      const devicesRef = DB.collection('devices');
      try {
        await PushNotifications.register();
        const r = await FCMPlugin.getToken();

        await devicesRef.doc(r.token).set({
          token: r.token,
          userId: state.user.id,
        });
      } catch (e) {
        console.log(e);
        toast('Push notifications register token error', 'danger');
      }
    };

    const subscribeToAnnouncements = async () => {
      try {
        await PushNotifications.register();
        await FCMPlugin.subscribeTo({ topic: 'announcements' });
      } catch (e) {
        toast('Push notifications subscribe error', 'danger');
      }
    };

    registerToken();
    subscribeToAnnouncements();
  }, [state.isAuth, state.user.id]);

  useEffect(() => {
    PushNotifications.addListener(
      'pushNotificationReceived',
      (notification) => {
        if (!state.isAuth) {
          return;
        }

        LocalNotifications.schedule({
            notifications: [
              {
                id: Date.now(),
                title: notification.title,
                body: notification.body,
              },
            ],
          });
      }
    );
  }, [state.isAuth]);

This works on Android and not on iOS, though the code was able to get the token on iOS but does not listen on pushNotificationReceived I guess since I am not receiving any notification on our device.

ugin to get FCM toke

Do we need the FCM token on both Android and iOS or only Android? And use APN on iOS? I am using https://github.com/stewwan/capacitor-fcm and have the same setup but I got it working only on android. See https://github.com/ionic-team/capacitor/issues/2795#issuecomment-616336303

removeAllListeners is only available in Capacitor 2.x

@jcesarmobile how about requestPermissions? Only in Capacitor 2.x too?

requestPermissions is Android only, it was in 1.x already.
But for requesting notification permission it's requestPermission, available on both LocalNotifications and PushNotifications

@jcesarmobile thank you. So requestPermission is required for iOS to start Push Notifs?

yes

yes

This is in Capacitor 2. So in Capacitor 1 maybe that's the reason that's why we are not receiving notifications using FCM in iOS but works in Android. Thank you.

In capacitor 1 calling register also requested the permission, but doesn't do it in Capacitor 2.

But please, don't keep asking questions on github issues.

For questions you can ask on the forum, slack or stack overflow using capacitor tag.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ebk46 picture ebk46  路  3Comments

moberwasserlechner picture moberwasserlechner  路  3Comments

stripathix picture stripathix  路  3Comments

nicobytes picture nicobytes  路  3Comments

MatanYadaev picture MatanYadaev  路  3Comments