cordova-plugin-firebase icon indicating copy to clipboard operation
cordova-plugin-firebase copied to clipboard

iOS: Push notifications are shown when in foreground

Open ghost opened this issue 7 years ago • 24 comments

On iOS Push notifications are shown although the app is in foreground. This bug was introduced in the commit https://github.com/arnesson/cordova-plugin-firebase/commit/06a8b2db25805ff9634f3b00dd9e3dcabf7d7f3e.

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.

ghost avatar Sep 03 '18 11:09 ghost

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.

rwoody avatar Sep 04 '18 15:09 rwoody

Also getting this issue :(

willmero avatar Sep 06 '18 04:09 willmero

@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.

briantq avatar Sep 07 '18 18:09 briantq

Made a pull request: https://github.com/arnesson/cordova-plugin-firebase/pull/835

ghost avatar Sep 08 '18 06:09 ghost

@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?

wildhart avatar Oct 16 '18 20:10 wildhart

Any updates on this? Still not working properly

pserna95 avatar Dec 17 '18 09:12 pserna95

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

cremfert avatar Dec 21 '18 20:12 cremfert

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).

cremfert avatar Dec 21 '18 21:12 cremfert

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

mrjavicho avatar Jan 20 '19 20:01 mrjavicho

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

baleyko avatar Feb 04 '19 15:02 baleyko

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: https://github.com/apache/cordova-ios/issues/407

angelbuzon avatar Feb 05 '19 14:02 angelbuzon

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

papattes avatar Mar 24 '19 19:03 papattes

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

Farmertjes avatar Apr 02 '19 12:04 Farmertjes

@Farmertjes where did u add this code?

piuskamil avatar Apr 14 '19 18:04 piuskamil

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

papattes avatar Apr 14 '19 20:04 papattes

@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?

papattes avatar Apr 22 '19 10:04 papattes

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).

cremfert avatar May 09 '19 11:05 cremfert

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

papattes avatar May 20 '19 17:05 papattes

It works for me

piuskamil avatar May 20 '19 17:05 piuskamil

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

papattes avatar May 20 '19 17:05 papattes

No. Its standard configuration. 2.0.5 it is

piuskamil avatar May 20 '19 17:05 piuskamil

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

papattes avatar May 20 '19 18:05 papattes

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)

cremfert avatar May 29 '19 12:05 cremfert

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.

gitdisrupt avatar Aug 21 '19 10:08 gitdisrupt