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: PushNotifications is missing the removeAllListeners function #2795

Closed wobsoriano closed 4 years ago

wobsoriano commented 4 years ago

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)

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

wobsoriano commented 4 years ago

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

yigitbacakoglu commented 4 years ago

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 .

wobsoriano commented 4 years ago

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?

yigitbacakoglu commented 4 years ago

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ā€™m pretty sure that all certificates, setup codes are in its place.

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

JoshuvaGeorge03 commented 4 years ago

@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; } });

wobsoriano commented 4 years ago

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ā€™m 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.

wobsoriano commented 4 years ago

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

jcesarmobile commented 4 years ago

removeAllListeners is only available in Capacitor 2.x

wobsoriano commented 4 years ago

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

jcesarmobile commented 4 years ago

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

wobsoriano commented 4 years ago

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

jcesarmobile commented 4 years ago

yes

wobsoriano commented 4 years ago

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.

jcesarmobile commented 4 years ago

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.

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.