Skip to content

[question]: Handling data (with content_available=true) notifications #1782

@max-baykalov

Description

@max-baykalov

How can we help?

We use OneSignal to handle notifications on Expo project. I faced an problem on version of react-native-onesignal: 5.2.5.

Code to catch notification in Expo app:

import { LogLevel, NotificationWillDisplayEvent, OneSignal } from 'react-native-onesignal';

...

const eventListener = (event: NotificationWillDisplayEvent) => {
    console.log(event);

    event.getNotification().display();
  };

  useEffect(() => {
    OneSignal.initialize(Constants?.expoConfig?.extra?.oneSignalAppId);
    OneSignal.Debug.setLogLevel(LogLevel.Verbose);
    OneSignal.Notifications.requestPermission(true);
    OneSignal.Notifications.addEventListener('foregroundWillDisplay', eventListener);

    return () => OneSignal.Notifications.removeEventListener('foregroundWillDisplay', eventListener);
  }, []);

The console.log in the eventListener method is triggered in case of data only notification. It works only when contents field is set. I assume this is correct behaviour because foregroundWillDisplay event must not be triggered when notification should not be displayed.

I need to handle data notifications when the expo app is in the foreground mode to refetch user's data. I send the following notification from the server:

Notification data

Notification {
  app_id: 'APP_ID',
  include_aliases: { external_id: [ 'USER_ID' ] },
  target_channel: 'push',
  data: { foo: 'bar' },
  content_available: true
}

Is there any way to intercept data notifications in JS code?

Thanks for any help.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions