React-native-onesignal: OneSignal.setNotificationWillShowInForegroundHandler() is not calling

Created on 16 Apr 2021  路  14Comments  路  Source: OneSignal/react-native-onesignal

I am receiving notification. I can see in Logs but below handler method never get called

 OneSignal.setNotificationWillShowInForegroundHandler((notifReceivedEvent) => {
            console.log("OneSignal: notification will show in foreground:", notifReceivedEvent);
            setTimeout(() => notifReceivedEvent.complete(), 0);
            let notification = notifReceivedEvent.getNotification();
            //onReceived(notification)
        });

I can see notification in logs

LOG NOTIFICATION: {"data": {"custom": "{\"a\":{\"messageBody\":\"\",\"messageId\":\"3e4f763d-2509-46e1-90b9-f39c99ef03a4\",\"type\":\"Command\",\"command\":\"WakeUp\"},\"i\":\"aa241d47-832e-4c7e-b7eb-f90dfcf88f1e\"}"}, "finish": [Function finish], "foreground": true, "id": "-547580619", "userInteraction": false}

Please guide what's wrong here ? Do I need to add handler before init SDK ?

FYI: I am using 4.0.6

Most helpful comment

I'm having the same issue.

All 14 comments

@EventFalconApp Could you provide the following details?

  1. What platform is the issue reproducible on (Android, iOS, both)?
  2. Provide Android and iOS versions test.
  3. Provide details about the notification sent from OneSignal.

    • Notification sent from OneSignal Dashboard with settings...

    • Notification sent from OneSignal REST API with payload...

  4. "LOG NOTIFICATION:" isn't something this SDK prints. Can you share the code you are using to print this?
  5. Are you setting a body on the notification?

    • A notification body is required otherwise the NotificationWillShowInForegroundHandler will not fire as it isn't a show event then.

Here is the info @jkasten2

  1. I tried in Android only.

  2. Android OS 10 (Stock Android) Nokia 7.1

3.1 If I sent message from Onesignal dashboard then NotificationWillShowInForegroundHandler method called and below is the notification in log

{"data": {"alert": "Testing", "custom": "{\"a\":{},\"i\":\"17daf10b-96c6-450e-a2d9-321ffa89c36a\"}", "pri": "10", "title": "Testing", "vis": "1"}, "finish": [Function finish], "foreground": true, "id": "1439721554", "userInteraction": false}

3.2 If I sent message by using API with server key then NotificationWillShowInForegroundHandler method is not calling and below is the notification in log

{"data": {"custom": "{\"a\":{\"messageBody\":\"\",\"messageId\":\"18b5cb22-e245-48a6-96b6-5f9b680b1594\",\"type\":\"Command\",\"command\":\"WakeUp\"},\"i\":\"b0ab4ee6-ae44-4ca8-8b89-5bd3b0ee36fa\"}"}, "finish": [Function finish], "foreground": true, "id": "1969504329", "userInteraction": false}

URL: https://onesignal.com/api/v1/notifications
Request body:

{
    "app_id": "XXX",
    "content_available": 1,
    "data": {
        "messageId": "18b5cb22-e245-48a6-96b6-5f9b680b1594",
        "type": "Command",
        "command": "Wakeup",
        "messageBody": ""
    },
    "include_external_user_ids": ["9847"]
}

  1. I shared logs in above points 3.1 & 3.2

  2. This is how back-end team created request body for creating notification

{
    "app_id": "XXX",
    "content_available": 1,
    "data": {
        "messageId": "18b5cb22-e245-48a6-96b6-5f9b680b1594",
        "type": "Command",
        "command": "Wakeup",
        "messageBody": ""
    },
    "include_external_user_ids": ["9847"]
}

@rgomezp Please help

one more

one more

Howdy,
So this is only occurring on notifications sent via the API?

Please provide the actual Javascript code where you are handling it.

@rgomezp Yes when I sent notification using API OneSignal.setNotificationWillShowInForegroundHandler() never get called

This is the code used to OneSignal setup

    OneSignal.setRequiresUserPrivacyConsent(false);
    OneSignal.setAppId(OneSignalAppId);
    OneSignal.setLogLevel(6, 0);
    if (isIos) {
        OneSignal.promptForPushNotificationsWithUserResponse(response => {
            console.log("Prompt response:", response);
        });
    }

Added 2 event listeners

  OneSignal.setNotificationWillShowInForegroundHandler((notifReceivedEvent) => {
        console.log("OneSignal: notification will show in foreground:", notifReceivedEvent);
        setTimeout(() => notifReceivedEvent.complete(), 0);
    });

    OneSignal.setNotificationOpenedHandler(notification => {
        console.log("OneSignal: notification opened");
    });

Unfortunately OneSignal.setNotificationWillShowInForegroundHandler() never get called

Howdy,
I think the problem you're seeing is that you're getting the notification received event but you are not "completing" the notification.

Please see the SDK reference for the correct way to actually display the notification. You have to pass the notification event as an argument to the complete function.

I am not passing anything to complete() because I want to treat it as silence notification.

Do you have any example ? I am doing as per demo

Is anything wrong in my request body ?

{
    "app_id": "XXX",
    "content_available": 1,
    "data": {
        "messageId": "18b5cb22-e245-48a6-96b6-5f9b680b1594",
        "type": "Command",
        "command": "Wakeup",
        "messageBody": ""
    },
    "include_external_user_ids": ["9847"]
}

I'm having the same issue.

@pbassut @SashaVons @manishpatelgt,
Are y'all also only seeing this issue with REST API notifications?

@rgomezp I'm using the REST API and I'm seeing this issue as well. I tried Android only and it is not working.

Here is my React Native Code

  async componentDidMount() {
    OneSignal.setLogLevel(6, 0)
    OneSignal.setAppId('xxx')

    OneSignal.setNotificationWillShowInForegroundHandler((notificationReceivedEvent) => {
      console.log('OneSignal: notification will show in foreground:', notificationReceivedEvent)

      const notification = notificationReceivedEvent.getNotification()
      notificationReceivedEvent.complete(notification)
    })

    OneSignal.setNotificationOpenedHandler((notification) => {
      console.log('OneSignal: notification opened:', notification)
    })
  }

Here is the API Request (rails)

    params = {
      app_id: 'xxx',
      contents: { en : some_object },
      include_external_user_ids : ['123']
    }

    response = HTTParty.post(
      'https://onesignal.com/api/v1/notifications',
      body: params.to_json,
      headers: {
        'Content-Type'  => 'application/json',
        'Authorization' => "Basic #{ ENV['ONESIGNAL_API_KEY'] }"
      }
    )

Hey all, I got notifications to work again.

I did yarn add @react-native-community/async-storage to update that package and notifications are working for me now.

Hey all, you need update version of "react-native-onesignal" to "4.1.1"

Was this page helpful?
0 / 5 - 0 ratings