Description:
Hello, my name is Eden and I encountered a problem:
I wanted to disable notifications on our app whenever the app is focused.
So I took this opportunity to update the onesignal to 4.0.3.
While using the new library, I saw that in order to achieve what I'm trying to achieve, I need to use:
setNotificationWillShowInForegroundHandler
So I implemented this function on our app:
OneSignal.setNotificationWillShowInForegroundHandler(notifReceivedEvent => {
notifReceivedEvent.complete();
});
In android it works fine, however, in IOS it still shows the notification.
I wanted to check if the code is getting there, so I added a wait function inside the arrow function, that waits 10 sec.
and it does wait, and gets into the function (of course, i did console.log as well haha).
Environment
Any ideas?
Thanks ahead,
Eden.
Howdy, can you please include detailed logs with the OneSignal log setting set to verbose?
@rgomezp Thanks for the fast response
Of course.
this is what i got on the xcode log:
2021-01-17 22:24:56.377019+0200 Landa[1198:526854] VERBOSE: finishProcessingNotification: Fired!
2021-01-17 22:24:56.377227+0200 Landa[1198:526854] VERBOSE: Notification display type: 0
2021-01-17 22:24:56.377415+0200 Landa[1198:526854] VERBOSE: notificationReceived called! opened: NO
2021-01-17 22:24:56.377780+0200 Landa[1198:526854] VERBOSE: finishProcessingNotification: call completionHandler with options: 0
I can't reproduce the issue unfortunately. Are you able to reproduce in the example app?
Was not able to bring the example on the ios at all haha,
however i debugged the objective C abit, to see if it gets there
it does goes to this line:
completion(nil);
and whenever it runs this line, it sends the message, like the complete null doesnt bother him
seriously no idea why :(
any suggestions?
Howdy,
Ok that's helpful. This sounds like a native issue then. @emawby
Thanks, ill be waiting for a response on this
➤ Elliot Mawby commented:
I am not able to reproduce this issue. In the Type script example app calling notifReceivedEvent.complete(); does not display the notification and calling notifReceivedEvent.complete(notif); does display the notification
This is odd because 2021-01-17 22:24:56.377780+0200 Landa[1198:526854] VERBOSE: finishProcessingNotification: call completionHandler with options: 0 This line in the log indicates that we correctly indicated to the OS that we want to silence the notification. Since we are not able to reproduce this issue with our example project it could be helpful if you are able to provide a project that is experiencing the issue
I have had the same issue (with android and ios).
I noticed that in the example at that the notifReceivedEvent.complete() was triggered in a setTimeout.
Replacing my code, which was identical to what @Eden-Eliel posted, by
OneSignal.setNotificationWillShowInForegroundHandler((notifReceivedEvent) => {
setTimeout(() => notifReceivedEvent.complete(), 0);
});
seems to solve the issue.
Seemed to work on my phone, but having same issue with a tester. Will probably get the chance to dig more on Tuesday.
Howdy,
The timeout is there just to show that you can put a delay on the notification if you would like. It shouldn't have any other impact on the notification behavior.
For that reason, I'm surprised that you say that setting a 0 timeout fixes it.
Can you please try it several times in both configurations to be 100% certain of this behavior?
Was wrong. Still having the issue. Sorry
Howdy,
No problem. Can you please include a full log while you reproduce the issue?
I have the same issue as well in iPhone 6.
It logs this line in Xcode:
VERBOSE: finishProcessingNotification: call completionHandler with options: 0
but still the notification is displayed when the app is open.
I really need a fix for this since my app is a chat/voice call app and cannot have lots of notifications constantly arriving during a call.
Howdy,
@thenderson55 thank you, but I'd like to see a full log from the device. Please see my previous comment which includes a link to the relevant docs.
Hi @rgomezp I'm also experiencing this issue. The linked docs to how to get a full log are related to crash logs, this does not produce a crash. Here is the full logging related to OneSignal I'm seeing when experiencing this issue
Xcode Logging:
2021-04-14 10:45:41.855888-0400 Gamebytes[11154:2949115] VERBOSE: onesignalUserNotificationCenter:willPresentNotification:withCompletionHandler: Fired! undefined: There
2021-04-14 10:45:41.898651-0400 Gamebytes[11154:2950554] [javascript] 'NOTIFICATION WILL SHOW IN FOREGROUND: ', false
2021-04-14 10:45:41.898693-0400 Gamebytes[11154:2950554] [javascript] SHOULD NOT SHOW NOTIFICATION
2021-04-14 10:45:41.898928-0400 Gamebytes[11154:2950794] VERBOSE: finishProcessingNotification: Fired!
2021-04-14 10:45:41.898979-0400 Gamebytes[11154:2950794] VERBOSE: Notification display type: 0
2021-04-14 10:45:41.899036-0400 Gamebytes[11154:2950794] VERBOSE: notificationReceived called! opened: NO
2021-04-14 10:45:41.899195-0400 Gamebytes[11154:2950794] VERBOSE: finishProcessingNotification: call completionHandler with options: 0
And here is the code for my foreground notification listener:
onNotificationShownInForeground = (notifReceivedEvent) => {
let notif = notifReceivedEvent.getNotification();
let userIsAlreadyInChat =
messagesStore.activeChatId === notif.additionalData.chatId;
let shouldShowNotification = !userIsAlreadyInChat;
console.log(
'NOTIFICATION WILL SHOW IN FOREGROUND: ',
shouldShowNotification
);
if (shouldShowNotification) {
notifReceivedEvent.complete(notif);
} else {
console.log('SHOULD NOT SHOW NOTIFICATION');
notifReceivedEvent.complete();
}
};
For my app this is a pretty severe issue, as we can't have notifications displaying for a user that's currently in the chat that the notifications are for.
For anyone dealing with this issue, I was able to fix it by removing the following code from my AppDelegate.m
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}
I'm not sure how exactly but it seems this was overriding OneSignal's implementation of this function and causing the issue. I had it implemented from the installation instructions from @react-native-community/push-notification-ios that I'm using for local notifications.
@rgomezp maybe something in the docs that implementing this method in your AppDelegate will cause issues would be helpful.
@maxbbb ,
Ah. That would be the problem.
When calling the completionHandler that way, you are essentially doing what the OneSignal SDK is facilitating using the OneSignal function. Calling the handler with those options _will_ result in the notification showing.
The OneSignal function will not override whatever is set there.
Thank you for figuring that out and contributing. We will make sure this is added to the documentation.
I will close this issue. If the above doesn't work for some, please comment and we will re-open in case of a different potential cause.
Most helpful comment
@maxbbb ,
Ah. That would be the problem.
When calling the
completionHandlerthat way, you are essentially doing what the OneSignal SDK is facilitating using the OneSignal function. Calling the handler with those options _will_ result in the notification showing.The OneSignal function will not override whatever is set there.
Thank you for figuring that out and contributing. We will make sure this is added to the documentation.