Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

[ios][messaging] onMessaggeReceived callback not triggered when showNotificationsWhenInForeground is true #1371

Closed
PeterStaev opened this issue Aug 21, 2019 · 7 comments

Comments

@PeterStaev
Copy link
Contributor

On iOS if I set showNotificationsWhenInForeground to true the onMessagReceivedCallback is not triggered when my app is in the foreground. Is it supposed to work like this? Can we make it so that the callback is also trigerred?

@PeterStaev
Copy link
Contributor Author

I think this is related to this code:

if (_showNotificationsWhenInForeground || // Default value, in case we always want to show when in foreground.
userInfoJSON["gcm.notification.showWhenInForeground"] === "true" || // This is for FCM, ...
userInfoJSON["showWhenInForeground"] === true || // ...this is for non-FCM...
(userInfoJSON.aps && userInfoJSON.aps.showWhenInForeground === true) // ...and this as well (so users can choose where to put it).
) {
// don't invoke the callback here, since the app shouldn't fi. navigate to a new page unless the user pressed the notification
completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
} else {
// invoke the callback here, since in this case 'userNotificationCenterDidReceiveNotificationResponseWithCompletionHandler' doesn't run
this.callback(notification);
completionHandler(0);
}
}

I think this is a wrong logic as there can be cases (like in my app right now) where you need to also process the callback AND show a notification when app is foreground. Considering under android the callback is always called.
@EddyVerbruggen can we at least have a config option that allows processing the callback in this case?

@EddyVerbruggen EddyVerbruggen added this to the 9.1.1 milestone Aug 27, 2019
EddyVerbruggen added a commit that referenced this issue Aug 27, 2019
@EddyVerbruggen
Copy link
Owner

EddyVerbruggen commented Aug 27, 2019

I guess that makes sense, and you can always still use the foreground property to decide what to do when a notification is received.. so I've now moved that callback invocation out of the condition:

public userNotificationCenterWillPresentNotificationWithCompletionHandler(center: UNUserNotificationCenter, notification: UNNotification, completionHandler: (p1: UNNotificationPresentationOptions) => void): void {
const userInfo = notification.request.content.userInfo;
const userInfoJSON = firebaseUtils.toJsObject(userInfo);
if (_showNotificationsWhenInForeground || // Default value, in case we always want to show when in foreground.
userInfoJSON["gcm.notification.showWhenInForeground"] === "true" || // This is for FCM, ...
userInfoJSON["showWhenInForeground"] === true || // ...this is for non-FCM...
(userInfoJSON.aps && userInfoJSON.aps.showWhenInForeground === true) // ...and this as well (so users can choose where to put it).
) {
completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
} else {
completionHandler(0);
}
this.callback(notification);
}

@ycherniavskyi
Copy link
Contributor

@EddyVerbruggen ups this fix has broken my app logic ☹️ - I want to execute push notification processing only when user click on it. So if a user receives a notification when the app is in the foreground it must be executed only if he clicks on it.

But for now, I can't figure out how to detect such behavior.
I can skip processing of first onMessaggeReceived callback by checking foreground property, but if the user clicks on it while the app still in foreground onMessaggeReceived executed with the same parameters.

Is it possible to distinguish these two situations with current implementation?
If not, is it possible to distinguish these two situations in general? If yes, I will create a separate issue and try to implement it (any advice for implementation direction are welcome).

@ditoglez
Copy link

@ycherniavskyi did you came up with a solution for this? I am experiencing the same issue in my app.

@ycherniavskyi
Copy link
Contributor

@ditoglez yes I came up, but with the really dirty solution, so don't tell anyone who shown it to you 🤣:

onMessageReceivedCallback: (message: firebase.Message) => {
    // ...
    if (message.foreground) {
        if (isIOS) { // HACK: to skip processing the notification which received in foreground check stack trace 
                     // (this processing appear after resolving the issue https://github.com/EddyVerbruggen/nativescript-plugin-firebase/issues/1371),
                     // the notification will remain in notification center because of `showNotificationsWhenInForeground` flag
                     // and will be processed on click on it
            const stack = new Error().stack;
            if (stack.includes("userNotificationCenterWillPresentNotificationWithCompletionHandler")) {
                console.log("onMessageReceivedCallback skipped")
                return;
            }
        }
        // ...
    }
    // ...
    processPushMessageHelper(message);
}

EddyVerbruggen added a commit that referenced this issue Oct 18, 2019
@EddyVerbruggen
Copy link
Owner

I've now (v 10.1.0) added the notificationTapped boolean to the notification payload on iOS. That may @ycherniavskyi to clean up that hacky (but cool!) logic.

@ycherniavskyi
Copy link
Contributor

@EddyVerbruggen thank you for the added flag, I replaced my hack with it and everything works as expected!

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

No branches or pull requests

4 participants