Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: fallbackToSettings YES not working good in 5.0.0 #1572

Closed
2 of 3 tasks
arnaudambro opened this issue Sep 28, 2023 · 5 comments
Closed
2 of 3 tasks

[Bug]: fallbackToSettings YES not working good in 5.0.0 #1572

arnaudambro opened this issue Sep 28, 2023 · 5 comments
Assignees

Comments

@arnaudambro
Copy link

arnaudambro commented Sep 28, 2023

What happened?

In SDK 5.0.0, when I use OneSignal.Notifications.requestPermission(true);, there is the prompt for notifications
If I accept, there is a new alert "your notifications are turned off, go to settings"

Steps to reproduce?

  1. Install v5.0.0
  2. Init OneSignal
  3. Call OneSignal.Notifications.requestPermission(true);
  4. Accept the notifications
  5. You should see this alert
You currently have notifications turned off for this application. You can open
Settings to re-enable them

Untitled 2

What did you expect to happen?

Not seeing this alert because I accepted the notifications

React Native OneSignal SDK version

5.0.0

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@nan-li nan-li self-assigned this Sep 28, 2023
@nan-li
Copy link
Contributor

nan-li commented Sep 28, 2023

Hi @arnaudambro, thanks for reporting.

I will try to reproduce this but have some questions for you:

  1. Is this a brand new app install?
  2. The only calls you made are OneSignal.initialize('YOUR_ONESIGNAL_APP_ID') and OneSignal.Notifications.requestPermission(true)? Nothing else?
  3. What is the device and iOS version?
  4. Does this happen all the time or randomly?

@arnaudambro
Copy link
Author

hi @nan-li

  1. yes
  2. no, I attach below my "service" file - I execute first OneSignalService.init before OneSignalService.promptForNotificationsPermission
  3. iPhone SE 3rd generation and iOS 16.6.1
  4. All the time
import { Linking } from 'react-native';
import { LogLevel, OneSignal } from 'react-native-onesignal';
class OneSignalService {
  isInit = false;
  async init() {
    try {
      /* O N E S I G N A L   S E T U P */
      OneSignal.Debug.setLogLevel(LogLevel.Verbose);
      OneSignal.setConsentRequired(false);
      this.isInit = true;

      /* O N E S I G N A L  S T A T U S E S */
      this.getOneSignalPushInfos('init');

      /* O N E S I G N A L  H A N D L E R S */
      // Add an observer
      OneSignal.Notifications.addEventListener('click', this.handleNotification);
      OneSignal.InAppMessages.addEventListener('click', this.handleInApp);
      this.triggerEvent('ONE_SIGNAL_INIT_DONE');
      return true;
    } catch (e) {
      console.log(e, { extra: { message: 'error init onesignal' } });
    }
    return false;
  }

  clean() {
    OneSignal.Notifications.removeEventListener('click', this.handleNotification);
    OneSignal.InAppMessages.removeEventListener('click', this.handleInApp);
  }

  handleInApp = (notification) => {
    if (notification?.click_url) Linking.openURL(notification?.click_url);
  };

  handleNotification = (notification) => {
    if (notification?.notification?.additionalData?.RedirectUrl) {
      Linking.openURL(notification?.notification?.additionalData?.RedirectUrl);
    }
  };

  listenToNotificationOnUseEffect = (listener) => {
    OneSignal.Notifications.addEventListener('click', listener);
    return () => {
      OneSignal.Notifications.removeEventListener('click', listener);
    };
  };

  checkNotificationPermission = async () => {
    const canAsk = await OneSignal.Notifications.canRequestPermission();
    const granted = OneSignal.Notifications.hasPermission();
    // BE CAREFUL: https://documentation.onesignal.com/docs/ios-provisional-push-notifications
    // As of 2023-09-18, `Enable iOS 12 direct to history` setting in OneSignal is enabled
    // It means, by default, when installing the app the first time, `granted === true` and `canAsk === true`
    // SO `granted === true` is not enough to know if the user has accepted notifications

    if (typeof granted?.permission == 'boolean') {
      return { canAsk, granted: granted?.permission };
    }
    return { canAsk, granted };
  };

  promptForNotificationsPermission = async () => {
    OneSignal.User.pushSubscription.optIn(); // not working, lol

    // if true, it should ask to open the settings if the user has already refused
    // but actually there is a bug: it asks to open the settings even if the user has already accepted
    // TODO: fix this bug when the lib is fixed -> https://github.com/OneSignal/react-native-onesignal/issues/1572
    // until now, fallbackToSettings is disabled on iOS thanks to patch package
    await OneSignal.Notifications.requestPermission(true);

    this.getOneSignalPushInfos('promptForNotificationsPermission');
  };

  logout = () => OneSignal.logout();

  triggerEvent = (event, props) => {
    try {
      if (!this.isInit) return;
      if (!props) {
        OneSignal.InAppMessages.addTriggers({ event });
      } else {
        OneSignal.InAppMessages.addTriggers({ event, ...props });
      }
    } catch (e) {
      console.log(e, { extra: { message: 'error trigger event onesignal', event, props } });
    }
  };
}

const OneSignalNotifications = new OneSignalService();
export default OneSignalNotifications;

@alexandreerick
Copy link

Same problem here, using version 4.4.0. This only happens on ios, i'm using iPhone 14 Pro (ios 16.2).

@arnaudambro
Copy link
Author

fixed for me

just remove the line OneSignal.User.pushSubscription.optIn(); and I made it work

@iamrohitagg
Copy link

Any other solution that this?

fixed for me

just remove the line OneSignal.User.pushSubscription.optIn(); and I made it work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants