capacitor icon indicating copy to clipboard operation
capacitor copied to clipboard

bug: pushNotificationReceived not called when app in background

Open spennyf opened this issue 5 years ago • 4 comments

Bug Report

Capacitor Version

💊 Capacitor Doctor 💊

Latest Dependencies:

@capacitor/cli: 2.4.6 @capacitor/core: 2.4.6 @capacitor/android: 2.4.6 @capacitor/electron: 2.4.6 @capacitor/ios: 2.4.6

Installed Dependencies:

@capacitor/cli 2.4.5 @capacitor/core 2.4.6 @capacitor/ios 2.4.6 @capacitor/android 2.4.6 @capacitor/electron not installed

[success] Android looking great! 👌 Found 8 Capacitor plugins for ios: cordova-annotated-plugin-android (1.0.4) cordova-plugin-apprate (1.7.1) cordova-plugin-dialogs (2.0.2) cordova-plugin-inappbrowser (4.1.0) cordova-plugin-nativestorage (2.3.2) cordova-plugin-purchases (2.0.0) cordova-plugin-x-socialsharing (6.0.3) es6-promise-plugin (4.2.2) [success] iOS looking great! 👌

Platform(s)

iOS

Current Behavior

I send a push notification from Firebase via the Java API, when my app is running and in the background (has NOT been killed), the push notification arrives on my device but pushNotificationReceived is not called. In the function I am making an API request to a health check API of mine so I can see if it got called. The function gets called when the app is in the foreground.

Expected Behavior

When the push notification arrives and the app is in the background (not killed), pushNotificationReceived should be called and exectued.

Code Reproduction

Other Technical Details

npm --version output: 6.14.8

node --version output: v14.15.1

pod --version output (iOS issues only): 1.10.1

Additional Context

spennyf avatar Feb 03 '21 01:02 spennyf

Hii spennyf, you have any news about this issue?

Thanks in advance!!

jon4ism avatar May 10 '21 08:05 jon4ism

Yeah, same issue here. Just want to make sure whether pushNotificationReceived should be called with data when app is in background. As a temp solution, I just refreshed the content when app state switched from background to foreground.

jl4386 avatar Jul 05 '21 10:07 jl4386

This would be very helpful to e.g. fetch data in background when a push notification is received, so the data can be displayed to the user immediately when foregrounding the app again.

mitschmidt avatar Sep 07 '21 14:09 mitschmidt

I'm trying to use this open issue to report my problem.

I'm using capacitor over a VueJS PWA. The listeners as provided in the guide are correctly triggered however the pushNotificationReceived listener returns an empty notification.

await PushNotifications.addListener('pushNotificationReceived', notification => {
                alert('Push received:', JSON.stringify(notification));
            });

so this is my code, the alert gets displayed but "notification" is empty. I'm trying to send "data" from the firebase console...the notification gets displayed correctly (for now I'm testing only iOS), but when I open the app the problem comes as described. I'm running out of ideas about where to find the problem.

In order to receive push notifications with images, I added to the ios project a Notification Service Extension with the following code. Thinking that was the problem I disabled the extension during build but the problem still occurs, alerts fires so the listener works, but notification is empty. Here is the extension code:


import UserNotifications
import Firebase

class NotificationService: UNNotificationServiceExtension {

    var contentHandler: ((UNNotificationContent) -> Void)?
    var bestAttemptContent: UNMutableNotificationContent?

    override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
        
        if let bestAttemptContent = bestAttemptContent {
            // Modify the notification content here...
			Messaging.serviceExtension().populateNotificationContent(self.bestAttemptContent!, withContentHandler: contentHandler)
            // contentHandler(bestAttemptContent)
        }
    }
    
    override func serviceExtensionTimeWillExpire() {
        // Called just before the extension will be terminated by the system.
        // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
        if let contentHandler = contentHandler, let bestAttemptContent =  bestAttemptContent {
            contentHandler(bestAttemptContent)
        }
    }

}

Any help or suggestion is appreciated. Thanks!

Nemesis19 avatar Jun 20 '22 21:06 Nemesis19

This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.

Please see the Contributing Guide for how to create a Code Reproduction.

Thanks! Ionitron 💙

Ionitron avatar Mar 06 '23 16:03 Ionitron

It looks like this issue didn't get the information it needed, so I'll close it for now. If I made a mistake, sorry! I am just a bot.

Have a great day! Ionitron 💙

Ionitron avatar Mar 22 '23 00:03 Ionitron

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

ionitron-bot[bot] avatar Apr 21 '23 00:04 ionitron-bot[bot]