OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.57k stars 374 forks source link

Notification shown despite setNotificationWillShowInForegroundHandler #1210

Closed alaindresse closed 3 years ago

alaindresse commented 3 years ago

Description:

I have noticed recently (not sure when this started) that on a fresh install, on Android, the notifications appear despite my calling complete() in setNotificationWillShowInForegroundHandler.

I can repeat the behaviour by uninstalling / resinstalling the app.

I do not have this issue on iOS.

I no longer have this issue once I click on one of the notifications.

Environment

extract from package.json "react-native": "0.63.4", "react-native-onesignal": "^4.0.7",

extract from package-lock.json "react-native-onesignal": { "version": "4.0.7", "requires": { "invariant": "^2.2.2" } },

installed with npm.

Have had the same issue with 4.0.8. Restored my files because everything is working on iOS and I want to avoid retesting everything.

Steps to Reproduce Issue:

  1. (your steps here)

Anything else:

(crash stacktraces, as well as any other information here)

My code to setup oneSignal:

import { useEffect, useState } from "react";
import { Platform } from "react-native";
import OneSignal, { NotificationReceivedEvent } from "react-native-onesignal";
import { useZusStore, ZusStore } from "../hooks/store/zustand-store";

export async function promptForPushPermission(): Promise<boolean> {
  const deviceState = await OneSignal.getDeviceState();
  if (deviceState.hasNotificationPermission === true) {
    return true;
  }
  if (Platform.OS === "ios") {
    return new Promise((resolve) => {
      OneSignal.promptForPushNotificationsWithUserResponse((allowed) => {
        resolve(allowed);
      });
    });
  } else {
    return false;
  }
}

export function clearPushUser(): void {
  OneSignal.removeExternalUserId();
}

function zusStoreReducer(state: ZusStore) {
  const { loggedUser, setNotifConversationId } = state;
  return {
    userId: loggedUser?.userId,
    setNotifConversationId
  } as {
    userId: string | undefined;
    setNotifConversationId: (conversationId?: string) => void;
  };
}

type NotifData = {
  conversationId?: string;
};

export function removePushExternalId() {
  OneSignal.removeExternalUserId();
}

function foregroundHandler(notifReceivedEvent: NotificationReceivedEvent) {
  notifReceivedEvent.complete();
}

export const useOneSignal = (): void => {
  const { userId, setNotifConversationId } = useZusStore(zusStoreReducer);
  const [notifData, setNotifData] = useState<NotifData>();

  useEffect(() => {
    console.log("setting up oneSignal");
    OneSignal.setAppId("XXXXXX");
    OneSignal.setNotificationWillShowInForegroundHandler(foregroundHandler);
    OneSignal.setNotificationOpenedHandler((openResult) => {
      const data = openResult.notification.additionalData;
      setNotifData(data);
    });
    OneSignal.setLogLevel(6, 0);
  }, []);

  useEffect(() => {
    if (userId) {
      OneSignal.setExternalUserId(userId);
    }
  }, [userId]);

  useEffect(() => {
    if (notifData?.conversationId) {
      if (setNotifConversationId !== undefined) {
        setNotifConversationId(notifData.conversationId);
        setNotifData(undefined);
      }
    }
  }, [notifData, setNotifConversationId]);
};

I captured all log messages containing OneSignal (from flipper), but hesitate to paste it here because it seems to contain ids I do not want to share in public. Happy to send it via private channel.

I noticed in the logs that OneSignal believes the app is in background (at 2021-05-06T07:28:39.072Z for instance) which was not the case.

After I opened the app by clicking on a push notification received, I no longer see this message.

Some extracts that seem meaningful:

First message, app in foreground but considered in background by OneSignal

[
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28444,
    "message": "Starting OneSignal initialization!\n",
    "date": "2021-05-06T07:28:38.612Z",
    "type": "verbose",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28444,
    "message": "No class found, not setting up OSRemoteNotificationReceivedHandler\n",
    "date": "2021-05-06T07:28:38.615Z",
    "type": "verbose",
    "count": 1
  }
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28444,
    "message": "OneSignal SDK initialization already completed.\n",
    "date": "2021-05-06T07:28:38.616Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29438,
    "message": "OSNotificationWorkManager enqueueing notification work with notificationId: a6df4e80-8b4a-4145-ae78-cb024eea59d4 and jsonPayload: {\"google.delivered_priority\":\"normal\",\"google.sent_time\":1620286116375,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"conversationId\\\":\\\"c15dc1e0-f1c1-48bb-9579-e1f1055699c0\\\"},\\\"i\\\":\\\"a6df4e80-8b4a-4145-ae78-cb024eea59d4\\\"}\",\"from\":\"340838961507\",\"alert\":\"Vous avez un nouveau message de Himoon\",\"google.message_id\":\"0:1620286116386515%1ed87b70f9fd7ecd\",\"google.c.sender.id\":\"340838961507\"}\n",
    "date": "2021-05-06T07:28:38.672Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29477,
    "message": "NotificationWorker running doWork with data: Data {json_payload : {\"google.delivered_priority\":\"normal\",\"google.sent_time\":1620286116375,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"conversationId\\\":\\\"c15dc1e0-f1c1-48bb-9579-e1f1055699c0\\\"},\\\"i\\\":\\\"a6df4e80-8b4a-4145-ae78-cb024eea59d4\\\"}\",\"from\":\"340838961507\",\"alert\":\"Vous avez un nouveau message de Himoon\",\"google.message_id\":\"0:1620286116386515%1ed87b70f9fd7ecd\",\"google.c.sender.id\":\"340838961507\"}, android_notif_id : 0, is_restoring : false, timestamp : 1620286118, }\n",
    "date": "2021-05-06T07:28:39.060Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29477,
    "message": "Running destroyTimeout with runnable: com.onesignal.OSNotificationReceivedEvent$1@bab0782\n",
    "date": "2021-05-06T07:28:39.069Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29477,
    "message": "Running startTimeout with timeout: 25000 and runnable: com.onesignal.OSNotificationReceivedEvent$1@bab0782\n",
    "date": "2021-05-06T07:28:39.069Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29477,
    "message": "remoteNotificationReceivedHandler not setup, displaying normal OneSignal notification\n",
    "date": "2021-05-06T07:28:39.069Z",
    "type": "warn",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29477,
    "message": "Running destroyTimeout with runnable: com.onesignal.OSNotificationReceivedEvent$1@bab0782\n",
    "date": "2021-05-06T07:28:39.070Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29477,
    "message": "Starting processJobForDisplay opened: false fromBackgroundLogic: true\n",
    "date": "2021-05-06T07:28:39.071Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29477,
    "message": "App is in background, show notification\n",
    "date": "2021-05-06T07:28:39.072Z",
    "type": "info",
    "count": 1
  }
]

Second message, app in foreground but considered in background by OneSignal

[
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28444,
    "message": "Starting OneSignal initialization!\n",
    "date": "2021-05-06T07:29:21.436Z",
    "type": "verbose",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28444,
    "message": "No class found, not setting up OSRemoteNotificationReceivedHandler\n",
    "date": "2021-05-06T07:29:21.438Z",
    "type": "verbose",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28444,
    "message": "OneSignal SDK initialization already completed.\n",
    "date": "2021-05-06T07:29:21.439Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29608,
    "message": "OSNotificationWorkManager enqueueing notification work with notificationId: b2cd61bc-bbbd-47b2-8710-e46b67a014d8 and jsonPayload: {\"google.delivered_priority\":\"normal\",\"google.sent_time\":1620286161680,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"conversationId\\\":\\\"c15dc1e0-f1c1-48bb-9579-e1f1055699c0\\\"},\\\"i\\\":\\\"b2cd61bc-bbbd-47b2-8710-e46b67a014d8\\\"}\",\"from\":\"340838961507\",\"alert\":\"Vous avez un nouveau message de Himoon\",\"google.message_id\":\"0:1620286161701524%1ed87b70f9fd7ecd\",\"google.c.sender.id\":\"340838961507\"}\n",
    "date": "2021-05-06T07:29:21.452Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29613,
    "message": "NotificationWorker running doWork with data: Data {json_payload : {\"google.delivered_priority\":\"normal\",\"google.sent_time\":1620286161680,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"conversationId\\\":\\\"c15dc1e0-f1c1-48bb-9579-e1f1055699c0\\\"},\\\"i\\\":\\\"b2cd61bc-bbbd-47b2-8710-e46b67a014d8\\\"}\",\"from\":\"340838961507\",\"alert\":\"Vous avez un nouveau message de Himoon\",\"google.message_id\":\"0:1620286161701524%1ed87b70f9fd7ecd\",\"google.c.sender.id\":\"340838961507\"}, android_notif_id : 0, is_restoring : false, timestamp : 1620286161, }\n",
    "date": "2021-05-06T07:29:21.586Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29613,
    "message": "Running destroyTimeout with runnable: com.onesignal.OSNotificationReceivedEvent$1@1efba65\n",
    "date": "2021-05-06T07:29:21.588Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29613,
    "message": "Running startTimeout with timeout: 25000 and runnable: com.onesignal.OSNotificationReceivedEvent$1@1efba65\n",
    "date": "2021-05-06T07:29:21.588Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29613,
    "message": "remoteNotificationReceivedHandler not setup, displaying normal OneSignal notification\n",
    "date": "2021-05-06T07:29:21.588Z",
    "type": "warn",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29613,
    "message": "Running destroyTimeout with runnable: com.onesignal.OSNotificationReceivedEvent$1@1efba65\n",
    "date": "2021-05-06T07:29:21.589Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29613,
    "message": "Starting processJobForDisplay opened: false fromBackgroundLogic: true\n",
    "date": "2021-05-06T07:29:21.589Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29613,
    "message": "App is in background, show notification\n",
    "date": "2021-05-06T07:29:21.591Z",
    "type": "info",
    "count": 1
  }
]

Third message, after having clicked on a notification. notificationWillShowInForegroundHandler called !

[
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28444,
    "message": "Starting OneSignal initialization!\n",
    "date": "2021-05-06T07:29:46.374Z",
    "type": "verbose",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28444,
    "message": "No class found, not setting up OSRemoteNotificationReceivedHandler\n",
    "date": "2021-05-06T07:29:46.377Z",
    "type": "verbose",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28444,
    "message": "OneSignal SDK initialization already completed.\n",
    "date": "2021-05-06T07:29:46.378Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29743,
    "message": "OSNotificationWorkManager enqueueing notification work with notificationId: ef64782a-a933-4c8b-9a4b-f24ce9ada530 and jsonPayload: {\"google.delivered_priority\":\"normal\",\"google.sent_time\":1620286186692,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"conversationId\\\":\\\"c15dc1e0-f1c1-48bb-9579-e1f1055699c0\\\"},\\\"i\\\":\\\"ef64782a-a933-4c8b-9a4b-f24ce9ada530\\\"}\",\"from\":\"340838961507\",\"alert\":\"Vous avez un nouveau message de Himoon\",\"google.message_id\":\"0:1620286186708211%1ed87b70f9fd7ecd\",\"google.c.sender.id\":\"340838961507\"}\n",
    "date": "2021-05-06T07:29:46.390Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29413,
    "message": "NotificationWorker running doWork with data: Data {json_payload : {\"google.delivered_priority\":\"normal\",\"google.sent_time\":1620286186692,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"conversationId\\\":\\\"c15dc1e0-f1c1-48bb-9579-e1f1055699c0\\\"},\\\"i\\\":\\\"ef64782a-a933-4c8b-9a4b-f24ce9ada530\\\"}\",\"from\":\"340838961507\",\"alert\":\"Vous avez un nouveau message de Himoon\",\"google.message_id\":\"0:1620286186708211%1ed87b70f9fd7ecd\",\"google.c.sender.id\":\"340838961507\"}, android_notif_id : 0, is_restoring : false, timestamp : 1620286186, }\n",
    "date": "2021-05-06T07:29:46.502Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29413,
    "message": "Running destroyTimeout with runnable: com.onesignal.OSNotificationReceivedEvent$1@989cdbe\n",
    "date": "2021-05-06T07:29:46.503Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29413,
    "message": "Running startTimeout with timeout: 25000 and runnable: com.onesignal.OSNotificationReceivedEvent$1@989cdbe\n",
    "date": "2021-05-06T07:29:46.504Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29413,
    "message": "remoteNotificationReceivedHandler not setup, displaying normal OneSignal notification\n",
    "date": "2021-05-06T07:29:46.504Z",
    "type": "warn",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29413,
    "message": "Running destroyTimeout with runnable: com.onesignal.OSNotificationReceivedEvent$1@989cdbe\n",
    "date": "2021-05-06T07:29:46.504Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29413,
    "message": "Starting processJobForDisplay opened: false fromBackgroundLogic: true\n",
    "date": "2021-05-06T07:29:46.505Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29413,
    "message": "Fire notificationWillShowInForegroundHandler\n",
    "date": "2021-05-06T07:29:46.507Z",
    "type": "info",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29413,
    "message": "Running destroyTimeout with runnable: com.onesignal.OSNotificationReceivedEvent$1@b50ae1f\n",
    "date": "2021-05-06T07:29:46.507Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 29413,
    "message": "Running startTimeout with timeout: 25000 and runnable: com.onesignal.OSNotificationReceivedEvent$1@b50ae1f\n",
    "date": "2021-05-06T07:29:46.507Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28685,
    "message": "Running destroyTimeout with runnable: com.onesignal.OSNotificationReceivedEvent$1@b50ae1f\n",
    "date": "2021-05-06T07:29:46.758Z",
    "type": "debug",
    "count": 1
  },
  {
    "tag": "OneSignal",
    "pid": 28444,
    "tid": 28685,
    "message": "Saving Notification job: OSNotificationGenerationJob{jsonPayload={\"google.delivered_priority\":\"normal\",\"google.sent_time\":1620286186692,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"conversationId\\\":\\\"c15dc1e0-f1c1-48bb-9579-e1f1055699c0\\\"},\\\"i\\\":\\\"ef64782a-a933-4c8b-9a4b-f24ce9ada530\\\"}\",\"from\":\"340838961507\",\"alert\":\"Vous avez un nouveau message de Himoon\",\"google.message_id\":\"0:1620286186708211%1ed87b70f9fd7ecd\",\"google.c.sender.id\":\"340838961507\"}, isRestoring=false, isIamPreview=false, shownTimeStamp=1620286186, overriddenBodyFromExtender=null, overriddenTitleFromExtender=null, overriddenSound=null, overriddenFlags=null, orgFlags=null, orgSound=null, notification=OSNotification{notificationExtender=null, groupedNotifications=null, androidNotificationId=-1, notificationId='ef64782a-a933-4c8b-9a4b-f24ce9ada530', templateName='', templateId='', title='null', body='Vous avez un nouveau message de Himoon', additionalData={\"conversationId\":\"c15dc1e0-f1c1-48bb-9579-e1f1055699c0\"}, smallIcon='null', largeIcon='null', bigPicture='null', smallIconAccentColor='null', launchURL='null', sound='null', ledColor='null', lockScreenVisibility=1, groupKey='null', groupMessage='null', actionButtons=null, fromProjectNumber='340838961507', backgroundImageLayout=null, collapseId='null', priority=0, rawPayload='{\"google.delivered_priority\":\"normal\",\"google.sent_time\":1620286186692,\"google.ttl\":259200,\"google.original_priority\":\"normal\",\"custom\":\"{\\\"a\\\":{\\\"conversationId\\\":\\\"c15dc1e0-f1c1-48bb-9579-e1f1055699c0\\\"},\\\"i\\\":\\\"ef64782a-a933-4c8b-9a4b-f24ce9ada530\\\"}\",\"from\":\"340838961507\",\"alert\":\"Vous avez un nouveau message de Himoon\",\"google.message_id\":\"0:1620286186708211%1ed87b70f9fd7ecd\",\"google.c.sender.id\":\"340838961507\"}'}}\n",
    "date": "2021-05-06T07:29:46.759Z",
    "type": "debug",
    "count": 1
  }
]
alaindresse commented 3 years ago

A workaround seems to be to move the lines

    OneSignal.setAppId("XXXXXX");
    OneSignal.setNotificationWillShowInForegroundHandler(foregroundHandler);

outside of the hook, i.e. as

OneSignal.setAppId("XXXXXX");
OneSignal.setNotificationWillShowInForegroundHandler(foregroundHandler);
function foregroundHandler(notifReceivedEvent: NotificationReceivedEvent) {
  notifReceivedEvent.complete();
}

export const useOneSignal = (): void => {
  const { userId, setNotifConversationId } = useZusStore(zusStoreReducer);
  const [notifData, setNotifData] = useState<NotifData>();

  useEffect(() => {
    OneSignal.setNotificationOpenedHandler((openResult) => {
      const data = openResult.notification.additionalData;
      setNotifData(data);
    });
  }, []);
// ...
}

Works in my case because I have a simple foreground handler, but would not work in all cases, I believe.

alaindresse commented 3 years ago

Could this be linked to #1200 ?

Jeasmine commented 3 years ago

Hello @alaindresse! Thanks for your issue report.

There are a few details that I want to check. If you don't click a notification after a fresh install, close and open the app, then receive a notification while the app is in the foreground, the NotificationWillShowInForegroundHandler is being called? I want to know if this behavior only happens on the first app opening after a fresh install.

The custom hook useOneSignal where is it being called?

Thanks again!

alaindresse commented 3 years ago

Hi Jeasmine,

Sorry for not coming back to you sooner. It seems indeed that this happens (most of the time, not systematically) only on a fresh install, and disappears once I have opened the app from a push notification. I say seems because it is not consistent (looks like a race condition). I have only had the issue with fresh installs lately, and only about once every 3 or 4 installs. The situation improved when I moved OneSignal.setAppId outside of the hook.

The custom useOneSignal hook is called very close to my root component (right after I have initialised my connection with my backend).

Alain

rgomezp commented 3 years ago

This does indeed look like the same issue as #1200.

In that issue, they report it only occurring with notifications sent via the REST API. Is this also your experience? What happens if you send to a test device via the dashboard?

slorber commented 3 years ago

For anyone having a similar error (also seen in the logs above).

Had a problem where this warning was displayed (when sending a push through the UI to a test device)

remoteNotificationReceivedHandler not setup, displaying normal OneSignal notification

The reason was that I was calling complete(notification) with a normalized/modified notification object. You must pass the original one, and this won't work:

    OneSignal.setNotificationWillShowInForegroundHandler((notificationReceivedEvent) => {
        const notification = normalize(notificationReceivedEvent.getNotification())
        setTimeout(() => notificationReceivedEvent.complete(notification), 0);
      },
    );

You could throw in such a case to make this mistake easier to avoid.

Also, I still have no idea why a setTimeout (found in doc) is needed here, and intercom support couldn't tell either

JayantJoseph commented 3 years ago

I am facing with a similar issue with setNotificationWillShowInForegroundHandler. I have written this handler in App.tsx file (root file) in our app (as suggested in the documentation).

At times, when the push notification arrives, setNotificationWillShowInForegroundHandler is not getting triggered. We noticed that this happens at times when we kill the android app and open it again, but this is not the only scenario in which this happens. Do note that push notification comes in all scenarios, but this handler is not getting triggered in some cases.

We have checked the documentations, and as said in it we have kept the handler in our root file. We also looked through reported issues in the package, tried downgrading react-native-onesignal package version to 4.1.1 (as said in issue 1200 ), but still this handler is not working as expected.

We have arrived to a conclusion that this is an issue of setNotificationWillShowInForegroundHandler, as it is not getting triggered every time a push comes when the app is in foreground. The code below is how our App.tsx looks like:

const App: React.FC = () => {
  const getOneSignalSubscriptionState = async (): Promise<void> => {
    const deviceState = await OneSignal.getDeviceState();
    dispatch(setOneSignalSubscribed(deviceState.isSubscribed)); // redux action dispatch
  };

  useEffect(() => {
    getOneSignalSubscriptionState();

    if (loggedIn) { // redux state defined when logged in
      OneSignal.setAppId(APP_ID);
      OneSignal.setLogLevel(6, 0); //
      OneSignal.setRequiresUserPrivacyConsent(false); //

      const externalUserId = isUserType1 ? userType1.profileUid
        : userType2.profileUid;

      OneSignal.setExternalUserId(externalUserId, (results) => {
        getOneSignalSubscriptionState();
      });
    }

    return () => {
      OneSignal.clearHandlers();
    };
  }, [loggedIn]);

  useEffect(() => {
    OneSignal.setNotificationWillShowInForegroundHandler((notifReceivedEvent) => {
      const notif = notifReceivedEvent.getNotification();

      const { notification } = notif.additionalData;

      const lastActiveNotification = {
        type: notification.redirection_type,
        data: notification
      };
      dispatch(setLastActiveNotification(lastActiveNotification)); // redux action dispatch

      if (notification.redirection_type === 'type-we-are-trying-to-silence') {
        notifReceivedEvent.complete();
      } else {
        notifReceivedEvent.complete(notif);
      }
    });
  });

  return (
    <>
      <StatusBar />
      <AppContainer />
    </>
  );
};

export default App;
rgomezp commented 3 years ago

Also, I still have no idea why a setTimeout (found in doc) is needed here, and intercom support couldn't tell either

@slorber , can you please link to the docs where you see this? We had this previously to show an example of something you could do but it is NOT required.

@JayantJoseph , Can you please provide the native logcat logs please?

slorber commented 3 years ago

@rgomezp it was probably fixed, can't fnd it back.

I think it was in these pages

rgomezp commented 3 years ago

Yes thanks @slorber

Closing this issue due to inactivity. Please comment to reopen @alaindresse @JayantJoseph

sweatherall commented 2 years ago

@rgomezp I am experiencing this same issue. RN version: 0.66.3 react-native-onesignal: 4.3.3 Testing on Android only.

I receive the notification on my phone, but setNotificationWillShowInForegroundHandler callback is never called.

I'm sending a test notification from the UI dashboard

@slorber @JayantJoseph @alaindresse did you ever get it working?