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
@EventFalconApp Could you provide the following details?
NotificationWillShowInForegroundHandler will not fire as it isn't a show event then.Here is the info @jkasten2
I tried in Android only.
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"]
}
I shared logs in above points 3.1 & 3.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"
Most helpful comment
I'm having the same issue.