-
Notifications
You must be signed in to change notification settings - Fork 374
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
How to get initial notification? #332
Comments
I have the same problem, and it seems like many others are also having this problem. Some related/duplicate issues I've found include #206, #195, #191, #336, #334, #264, #279. These issues all seem to be about this problem of the If you move this code so that it happens as early as possible, it appears that the event listener will be registered before the event is fired and as such your event handler function will be called. For example, you should move the event listener registration code to your Once you've got this set up, you're now able to capture the event that a notification was opened and its up to you how you handle it, which will depend on how your app is set up (e.g. whether you're using Redux, or whether you're using redux-persist, etc.). My current solution is to save the notification to a variable defined at the top level of my This solution is obviously very hacky, and there are probably some edge cases where this would not work as it's supposed to. But until a proper solution is implemented in react-native-onesignal, this seems to be a decent workaround. I think what needs to happen is for react-native-onesignal to be updated so that it waits until you've set up your event handlers before it fires off the events. One possible solution could be that when events occur (such as the Another possible solution, at least for specifically detecting that the app was opened because the user tapped on a push notification, would be to do something similar to So I think there really needs to be a solution that addresses this core problem of events being fired before any corresponding event listener is registered, leading to the event being missed by the app. |
Hi @jordanmkoncz , can you share the code of the file to which you dispatched the actions? I have the same react-native setup as you (redux-persist etc..) |
@ccoeder sure, here's my current import React, { Component } from 'react';
import { Provider } from 'react-redux';
import OneSignal from 'react-native-onesignal';
import isNil from 'lodash/isNil';
import configureStore from '../redux/configureStore';
import {
pushNotificationIdsReceived,
pushNotificationOpened,
pushNotificationReceived,
pushNotificationRegistered,
} from '../redux/actionCreators';
import AppNavigator from './AppNavigator';
import Blank from './Blank';
let openedPushNotificationResult = null;
let receivedPushNotification = null;
let receivedNotificationUserInfo = null;
let receivedIds = null;
const onOpened = openResult => {
openedPushNotificationResult = openResult;
};
const onReceived = notification => {
receivedPushNotification = notification;
};
const onRegistered = notificationUserInfo => {
receivedNotificationUserInfo = notificationUserInfo;
};
const onIds = ids => {
receivedIds = ids;
};
OneSignal.addEventListener('opened', onOpened);
OneSignal.addEventListener('received', onReceived);
OneSignal.addEventListener('registered', onRegistered);
OneSignal.addEventListener('ids', onIds);
class App extends Component {
constructor() {
super();
this.state = {
isStoreInitialised: false,
};
this.store = null;
this.onOpened = this.onOpened.bind(this);
this.onReceived = this.onReceived.bind(this);
this.onRegistered = this.onRegistered.bind(this);
this.onIds = this.onIds.bind(this);
}
componentDidMount() {
this.store = configureStore(store => {
if (!isNil(openedPushNotificationResult)) {
store.dispatch(pushNotificationOpened(openedPushNotificationResult));
}
if (!isNil(receivedPushNotification)) {
store.dispatch(pushNotificationReceived(receivedPushNotification));
}
if (!isNil(receivedNotificationUserInfo)) {
store.dispatch(pushNotificationRegistered(receivedNotificationUserInfo));
}
if (!isNil(receivedIds)) {
store.dispatch(pushNotificationIdsReceived(receivedIds));
}
OneSignal.addEventListener('opened', this.onOpened);
OneSignal.addEventListener('received', this.onReceived);
OneSignal.addEventListener('registered', this.onRegistered);
OneSignal.addEventListener('ids', this.onIds);
OneSignal.removeEventListener('opened', onOpened);
OneSignal.removeEventListener('received', onReceived);
OneSignal.removeEventListener('registered', onRegistered);
OneSignal.removeEventListener('ids', onIds);
this.setState({ isStoreInitialised: true });
});
}
componentWillUnmount() {
OneSignal.removeEventListener('opened', this.onOpened);
OneSignal.removeEventListener('received', this.onReceived);
OneSignal.removeEventListener('registered', this.onRegistered);
OneSignal.removeEventListener('ids', this.onIds);
OneSignal.removeEventListener('opened', onOpened);
OneSignal.removeEventListener('received', onReceived);
OneSignal.removeEventListener('registered', onRegistered);
OneSignal.removeEventListener('ids', onIds);
}
onOpened(openResult) {
this.store.dispatch(pushNotificationOpened(openResult));
}
onReceived(notification) {
this.store.dispatch(pushNotificationReceived(notification));
}
onRegistered(notificationUserInfo) {
this.store.dispatch(pushNotificationRegistered(notificationUserInfo));
}
onIds(ids) {
this.store.dispatch(pushNotificationIdsReceived(ids));
}
render() {
if (!this.state.isStoreInitialised) {
return <Blank />;
}
return (
<Provider store={this.store}>
<AppNavigator />
</Provider>
);
}
}
export default App; Note: So basically, I wait until my store has been set up and rehydrated, and then I dispatch actions based on events received by my initial event listeners, then add the new event listeners and remove the initial event listeners. |
@jordanmkoncz Thank you for sharing your code. At first I thought I had the same error as you, but I realized that my problem is related to the silent notification. I need to take action if the notification comes while the application is closed. I guess your codes do not work for me. |
No worries @ccoeder. Yeah the issue of actually taking action on a notification while the app is closed is a different one. This issue is specifically relating to detecting in the app that the user tapped a notification to open the app, at the time that the app is launched (when it was previously not running at all). |
Take a look how react-native-fcm does, there is an method called getInitialNotification |
@jordanmkoncz Thanks for your posting. I had the similar issue. when App received the push notification, screen navigates to the desired screen. What do you think about this? If you can help me, hope to please reply here. Thanks |
Hmm.. I have my
Even if my app if closed -- not in a |
@wkoutre that's interesting, could you post what versions of Could you do some testing to see if it still works 100% of the time if you artificially delay the loading of your |
I've tested on both iOS and Android, yes. On both, 100% of the time:
I'll do the EDIT1: I changed absolutely nothing, and now it fails on Android 100% of the time. EDIT2: Doing the Not sure why Android is inconsistent, but it's obviously an issue. On Android, when the app is completely closed and a notification comes in, the debugger clears and everything in my
|
@jordanmkoncz Curious if you've tried this library: https://github.com/wix/react-native-notifications |
Alright I have a workaround that works 100% of the time on both iOS and Android. In my
... where Then, when my (now mounted) App finishes rehydrating, I call:
My App -- wrapped with a redux
From there, the @bhoop It's been a couple months, so not sure if you're still looking for a fix but... if so, give this a try. |
@wkoutre Could you set up a PR with updates to the Readme file that includes this workaround for redux? |
@avishayil Sure thing. I’ll get to it by this evening |
@avishayil PR made. |
I'm facing a problem that sounds like this. When app is opened, it's running ok all callbacks, when the app is in background as well. But, if I receive a push, my app icon increment +1, and the system show it's banner with notification content BUT if I ignore the banner and open the app by the icon (not by notification banner) callback is ignored =[ how to handle this ?
|
@brunoandradebr this is the expected behavior. Opening the app from the app icon never takes in account any notification you may have received. It just opens the app. If you want to change the behavior of your app when it starts based on whether the user received a notification, you'll need to check that yourself on launch. |
It's not true. When I open the app by icon when the app is running in background, it's handles push notification as desired. Only when I remove it from memory (swipe), then I open by icon, nothing happens. I was investigating and I think it's XCode stop the app and closes oonesignal lib link. |
@brunoandradebr I might be misunderstanding the issue you're having, and which callback(s) are you referring to. |
When I kill the app and has a notification, if I open the app by icon, the app can't handle callback events, BUT if the app is running on background, and I open by icon, callbacks fires as expected. Got it ? |
@brunoandradebr I think I got it, and if I'm not wrong that's the behavior I described in my previous comment. In any case, I feel this is off topic and if you think you have an issue with this, you should open a separate issue on the repo. |
@jordanmkoncz You seem to have a really good understanding of the react native onesignal issues. The versions I am using react-native-onesignal: ^3.2.8 and react-native: 0.56.0. I was able to implement handling notifications in the background and foreground but I am running into a problem now when the app is closed and a notification is tapped. The app only shows a blank white screen that does not go away unless you close and restart it. I am not sure how to handle this situation since I cannot find any documentation on OneSignal's website. It is frustrating because I spent time learning and implementing this library and now I am not sure if it will be able to handle this extremely important use case. I read a possible workaround for this above which implementing the event listeners in index.js but I am using react navigation and need to get a reference from my main navigator which is declared in App.js in order to navigate to a nested stack screen. I would really appreciate any advice that one has in dealing with this problem and how they solved it. Thanks in advance. |
The React Native
PushNotificationIOS
module has agetInitialNotification()
method that allows fetching the notification that was tapped on to open the app (if there was one). Is there some similar functionality for this library?When I sent a test notification to my device while the app was closed I received the
Device info:
log in my console when it opened, but I didn't see any of the other event logs for receiving a notification.Edit 3:
For some reason I'm no longer able to see the
onOpened
event for initial notifications, though nothing changed in my code.Edit 2:
By moving around the listeners I was able to see the log for the
onOpened
event, but that being a passive listener means that I can't actively check to see on launch if my app was opened with a notification or not, I just have to wait for some arbitrary period of time to see if that event is fired? Is there no way to actively check whether the app was opened with a push notification or not?Edit:
I found an article that references using
OneSignal.configure()
andonNotificationOpened
, but looking at the source code it doesn't appear thatOneSignal.configure()
accepts parameters anymore?The text was updated successfully, but these errors were encountered: