Skip to content
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

iOS: Push notifications are shown when in foreground #817

Open
ghost opened this issue Sep 3, 2018 · 24 comments
Open

iOS: Push notifications are shown when in foreground #817

ghost opened this issue Sep 3, 2018 · 24 comments

Comments

@ghost
Copy link

ghost commented Sep 3, 2018

On iOS Push notifications are shown although the app is in foreground. This bug was introduced in the commit 06a8b2d.

As per Apple developer documentation: https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate

func userNotificationCenter(UNUserNotificationCenter, willPresent: UNNotification, withCompletionHandler: (UNNotificationPresentationOptions) -> Void)
Asks the delegate how to handle a notification that arrived while the app was running in the foreground.

So the completion handler should be called with completionHandler(UNNotificationPresentationOptionNone);, which doesn't show the Push notification anymore when the app is in foreground.

I think the default behavior of this plugin should be not to show Push notification when in foreground.

@rwoody
Copy link

rwoody commented Sep 4, 2018

I was relieved when they started working as I was not able to get this plugin to work with cordova-plugin-local-notifications; however, you are right that the notifications should not be shown as it was before.

@willmero
Copy link

willmero commented Sep 6, 2018

Also getting this issue :(

@briantq
Copy link
Contributor

briantq commented Sep 7, 2018

@maitscha it seems that you have some idea on a fix. Can you fork the repo, make the proposed change and create a pull request? That would also allow others to test the change.

@ghost
Copy link
Author

ghost commented Sep 8, 2018

Made a pull request: #835

@wildhart
Copy link

@maitscha, looking at the comments on #835 it looks like this bug isn't fixed by that PR. Any other ideas for how to prevent the notifications showing on iOS when the app is in the foreground?

jfougere added a commit to jfougere/cordova-plugin-firebase that referenced this issue Nov 9, 2018
…ation-foreground

Hide iOS push notifications when app is in foreground
@pserna95
Copy link

pserna95 commented Dec 17, 2018

Any updates on this? Still not working properly

@cremfert
Copy link

We are having the same issue - is there any progress made on this?

@cremfert
Copy link

I got it working.
Maybe this is helpful for someone: As I already had the plugin installed, I not only had to change PROJECT_ROOT/plugins/cordova-plugin-firebase/AppDelegate+FirebasePlugin.m but also PROJECT_ROOT/platforms/ios/PROJECT_NAME/plugins/cordova-plugin-firebase/AppDelegate+FirebasePlugin.m as changes to the former file are not automatically applied to the build (I guess you have to run cordova prepare ios, but this also removes the platform completely and re-adds it). Now, notifications are working as expected (not shown in foreground).

@mrjavicho
Copy link

same issue here. @cremfert what exactly did you change to make it work?

@baleyko
Copy link

baleyko commented Feb 4, 2019

Our team has faced with this bug too. Did someone handle that?

@angelbuzon
Copy link

angelbuzon commented Feb 5, 2019

if i rewrite AppDelegate+FirebasePlugin.m:
completionHandler(UNNotificationPresentationOptionAlert);
to:
completionHandler(UNNotificationPresentationOptionNone);

push notifications not appear, no matter if in foreground or in background.
please, can anyone tell us how to make it work?

Is this plugin outdated? There are no updates in months.

EDIT: solution: apache/cordova-ios#407

@papattes
Copy link

papattes commented Mar 24, 2019

Someone has a solution?
I tried changing "UNNotificationPresentationOptionAlert" to "UNNotificationPresentationOptionNone" in AppDelegate+FirebasePlugin.m ... without success :'(

@Farmertjes
Copy link

Farmertjes commented Apr 2, 2019

I have a fix for this bug

NSNumber *background = self.applicationInBackground;
    
if([background boolValue]) {
    completionHandler(UNNotificationPresentationOptionAlert);
} else {
    completionHandler(UNNotificationPresentationOptionNone);
}

** edit **

This should replace

completionHandler(UNNotificationPresentationOptionAlert);

on line 183 of the AppDelegate+FirebasePlugin.m

@piuskamil
Copy link

@Farmertjes where did u add this code?

@papattes
Copy link

@kamilo81 in the AppDelegate+FirebasePlugin.m file at line 183 it seems to me

@papattes
Copy link

@Farmertjes when I apply your solution, notifications are always displayed when the app is active. In addition, when the app is not open (no process in background or foreground) notifications are not processed.

Could you give a little more detail on what you did to make it work?

@cremfert
Copy link

cremfert commented May 9, 2019

I changed
PROJECT_ROOT/plugins/cordova-plugin-firebase/src/ios/AppDelegate+FirebasePlugin.m but also PROJECT_ROOT/platforms/ios/PROJECT_NAME/plugins/cordova-plugin-firebase/AppDelegate+FirebasePlugin.m.

In both files in line 183, I replaced
completionHandler(UNNotificationPresentationOptionAlert); with
completionHandler(UNNotificationPresentationOptionNone);

Now, it behaves like expected (no notification in foreground, only in background).

@papattes
Copy link

@cremfert there is no effect after these changes :'(

@piuskamil
Copy link

It works for me

@papattes
Copy link

@kamilo81 Do you have a special application configuration? This is the version 2.0.5 you use?

@piuskamil
Copy link

No. Its standard configuration. 2.0.5 it is

@papattes
Copy link

papattes commented May 20, 2019

The notification is not intercepted by this plugins!
There is a conflict with cordova-plugin-mauron85-background-geolocation 3.0.1 plugins...

@cremfert
Copy link

cremfert commented May 29, 2019

Not sure, why this happens - I just installed the plugin and changed the mentioned files. After a new build, everything works for me. I'm using 2.0.5 as well. Push messages are received and shown when the app is in the background, but not when in foreground. In the latter case, only the callback in the is called with the data of the push message.

EDIT: Maybe it's due to the conflict - I'm not using any background mode plugins (like the one mentioned in your post @papattes)

@gitdisrupt
Copy link

gitdisrupt commented Aug 21, 2019

I have a fix for this bug

NSNumber *background = self.applicationInBackground;
    
if([background boolValue]) {
    completionHandler(UNNotificationPresentationOptionAlert);
} else {
    completionHandler(UNNotificationPresentationOptionNone);
}

** edit **

This should replace

completionHandler(UNNotificationPresentationOptionAlert);

on line 183 of the AppDelegate+FirebasePlugin.m

This solved the issue for me as well. Is there any reason the Firebase team has not addressed this yet?
Could it be that rather than testing for background, is there a parameter we could be including in the payload instead? Or, could a solution involve changing configuration in XCode instead?
It would be amazing to see if anyone from Firebase has feedback. Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests