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]: OneSignal.Notifications.addEventListener on 'click' not firing when app is killed #1009

Closed
2 of 3 tasks
perfiljevmaksym opened this issue Jul 8, 2024 · 1 comment
Closed
2 of 3 tasks

Comments

@perfiljevmaksym
Copy link

perfiljevmaksym commented Jul 8, 2024

What happened?

When i click on received notification while app is killed. OneSignal.Notifications.addEventListener('click') not firing.
Otherwise, i app is active all work perfectly.

Tested this issue on Android 13, 9.

Init process OneSignal

  const handleNotificationClick = async (event: NotificationClickEvent): Promise<void> => {
    // @ts-ignore-next-line
    const twinId = event?.notification?.additionalData?.twin_id;
    alert(`handleNotificationClick event - ${JSON.stringify(event)}`);
  };

  async function initOneSignalNotifications(): Promise<void> {

        try {
          await PushNotifications.requestPermission();
          console.log('Notification permission granted');
        } catch (err) {
          console.log('Notification permission denied: ', err);
        }

        OneSignal.Notifications.addEventListener(
          'foregroundWillDisplay',
          async (event) => {
          `handleNotificationClick event - ${JSON.stringify(event)}`
        );

        OneSignal.Notifications.addEventListener(
          'click',
          handleNotificationClick,
        );
  }

  const initOneSignal = async (
    userId: string,
    onboarded: boolean,
  ): Promise<void> => {
    if (isPlatform('mobile')) {
      OneSignal.initialize(config.onesignal.appId);
      OneSignal.login(userId);

      if (onboarded) {
        await initOneSignalNotifications();
      }
    }
  };

Dependencies:

  • "onesignal-cordova-plugin": "^5.2.3",
  • "@capacitor/core": "^5.3.0",
  • "react": "^18.2.0",

Manifest Permissions:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="com.google.android.gms.permission.AD_ID" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Steps to reproduce?

1. Install latest cordova SDK
2. Add OneSignal.Notifications.addEventListener('click', /* callback*/)
3. Click on notification while app is killed

What did you expect to happen?

I expected to get notification click event and run callback on it

OneSignal Cordova SDK version

5.2.3

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
@perfiljevmaksym
Copy link
Author

perfiljevmaksym commented Jul 9, 2024

Resolved:
OneSignal is order sensitive

How to Init OneSignal and Listeners:

  1. Init OneSignal
OneSignal.initialize(config.onesignal.appId);
      OneSignal.login(userId);
  1. Setup Listeners like 'click', 'foregroundWillDisplay'

  2. Request Permission. Better call requestPermission in try catch

  3. Add to Manifest for Android >= 13
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

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

1 participant