Closed alaindresse closed 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.
Could this be linked to #1200 ?
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!
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
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?
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
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;
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?
@rgomezp it was probably fixed, can't fnd it back.
I think it was in these pages
Yes thanks @slorber
Closing this issue due to inactivity. Please comment to reopen @alaindresse @JayantJoseph
@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?
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:
Anything else:
(crash stacktraces, as well as any other information here)
My code to setup oneSignal:
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
Second message, app in foreground but considered in background by OneSignal
Third message, after having clicked on a notification. notificationWillShowInForegroundHandler called !