onReceived is not called when app is closed/swiped away
Hi, i am trying to store the notification received via onesignal and onNotificationReceived i have a function that writes to the device storage . Everything works as it is supposed to when the app is in the background orforeground but when the app is stopped in my case by swiping away from the list of running apps, the notification is displayed on the status bar but the onNotificationReceived function is not called. I tried moving the logic of storing the notification on the device to onNotificationOpened but still did not get the result i expected.
Have you find solution? I meet the same
No i have not found any solutions to this issue yet. I tried setting the priority of the notification to 10 which displays the notification on the device even if it is closed by swiping away. But it does not let you run any native code on the device through onReceived function. The solution might be to write some native java and ios code to make it work but not sure yet.
@softCompound You are correct, in order to get this even when the app is closed you will need to setup native code with Objective-C / Swift on iOS and Java / Kotlin on Android.
See OneSignal's Background Notifications for details on this.
@jkasten2 So do I understand this right, the react-native-onesignal package does not support background notifications and handling them? Because what I see currently is the following:
- I have set up the package in my Android app
- I get notifications when the App is open or in the background
- but when it's "dead" I do not get one.
Any help/clue? Or is this not related to this issue at all?
Yes, it is a related issue. When the app is dead i.e. swiped away from background tray, notification won't be received from one signal. If you set the notification to a higher priority say 10, the app should receive the notification. But as suggested above by @jkasten2 , we can add native java/kotlin for android which I have done following the link above. And now even if my app is closed, it still receives the notification however it does not execute any app logic to save the notification yet.
Hi @softCompound,
thanks for the quick reply.
(...) If you set the notification to a higher priority say 10, the app should receive the notification
It looks like this is working in Android 8 (app is closed), but not on Android 5 (closed too).
So will the Background Notifications docu enable it on Android 5 too? What do you guess/know about that?
(...) however it does not execute any app logic to save the notification yet.
you mean when the user clicks on this notification (which opens the app) and the app should do something with this notification?
Yeah, it did strike me as setting higher priority fix to be not a reliable solution. However, although not tested on devices operating on lower android versions, I think writing the native java for this feature is much more reliable as in the process we have to declare a service in the manifest.xml file. And as per android documentation a service can run in the background indefinitely, even if the component that started it is destroyed.
With regards to saving the notification, I want to save it before the user opens the notification by tapping it. So, something like when the device first receives the notification -> wake it up -> execute the logic to save notification to the device.
@softCompound Okay, I will need to check out the native code, but the link above brings me to an iOS related page. Do you have one for Android too for me? Or is this (Android Customizations) the right one already?
Another question: So do I get it right, I have to set up a service (do you mean these services?) on Android to make this work?
Yes, you have got the correct page for Android. The setup is done when you declare the class extending one signal-service class as a service element in the manifest.xml file.
The setup is done when you declare the class extending one signal-service class as a service element in the manifest.xml file.
You mean as described in the react-native documentation?
(thanks for your time, I am new to react-native and Android development :) )
No worries! It is pretty simple really. Just follow the steps 1, 2 and 3 under the section: Background Data and Notification Overriding and that is the minimum setup done for receiving background notification.
@softCompound So you mean step 1-3 here. How do I get this data into the React-Native Part afterwards/then?
@natterstefan I am not sure if you can actually get the data with react-native or native ios/android code when an app is not in the background. Perhaps due to privacy concerns, not sure. But yeah it would be interesting to see if there is an actual way to implement such logic.
@natterstefan did you have luck in getting the notification data back into react native part?
@V3RT1AG0 yes, kind of. Because I stopped using onReceived but used onOpened instead, I was able to achieve the same result I wanted to have. The latest version of react-native-onesignal works great for this. This is how I handle it (even works when the app is closed an one clicks on a notification):
First you have to add a listener (eg. in the componentWillMount of your App, currently I use it with React 16.2.0, but be aware that componentWillMount will become a legacy feature)
// app.js
componentWillMount () {
addOneSignalListener(this.onOneSignalListener)
}
Then in my OneSignal helper I add the listeners and handle the opened event from OneSignal.
//onesignal.js
export const addOneSignalListener = (listener) => {
OneSignal.addEventListener('opened', (event) => { onOpened(event, listener) })
}
const onOpened = (openResult, listener) => {
const data = get(openResult, 'notification.payload.additionalData', {})
listener('onOpened', data)
}
And the actual listener in app.js (who handles UI changes or other actions) can look similar to this:
// app.js
onOneSignalListener (event, data) {
if (event === 'onOpened' && data) {
// do something with the data of the notification
}
}
onOneSignalListener can also handle other events (eg. onReceived) if you create an onReceived function in onesignal.js and handle the event similar there.
You can read more about this here:
- https://documentation.onesignal.com/v5.0/docs/react-native-sdk-setup#section-usage
- https://documentation.onesignal.com/v5.0/docs/react-native-sdk-setup#section-handling-notifications
@natterstefan Hi Stefan
- Did you get onOpened to be called when the app was closed?
- Did you implement https://documentation.onesignal.com/docs/android-customizations#section-background-data-and-notification-overriding?
- Also, I am not sure what your code achieved, compared to the code that is described in https://documentation.onesignal.com/v5.0/docs/react-native-sdk-setup#section-usage. Can you explain, please?
Thanks Yossi
Hi @rahamin1,
sorry that it took me so long to answer. I am sorry I have no access to the codebase anymore, but the one I attached and described above was the one that was part of the App I worked with.
As far as I remember the onOpened did work and we were able to change the state of the MainApp for instance. We did not implement the Android-Customizations you linked to. We just followed the react-native-sdk-setup guide.
The latest version of react-native-onesignal we used in the project was 3.0.x. Maybe this helps you to debug/solve your issue.
Have you successfully implemented the Android and iOS setup steps? Note: We did not develop an iOS app, so I can not tell you something about it.
@natterstefan I folowed the steps you mention here, but it still not displaying the notification.
I opened Android Monitor on Android Studio and when I fire a notification with the app dead I can see this output:
09-14 16:41:26.092 1179-1281/? W/BroadcastQueue: Reject to launch app com.sicredimobile/10516 for broadcast: App Op 64
09-14 16:41:26.093 1179-1281/? W/BroadcastQueue: Reject to launch app com.sicredimobile/10516 for broadcast: App Op 64
09-14 16:41:26.095 20048-20048/? W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.sicredimobile (has extras) }
Did you have any idea of how can I solve this?
Hi @rootedy-ffit. No, I have no idea what this is about and what caused this output/error. Sorry.