question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android] tapping push notification does nothing when app not running or in background

See original GitHub issue

Description of the problem:

Affected platform

  • Android
  • iOS
  • electron
  • web

OS of the development machine

  • Windows
  • macOS
  • linux

Other information: This is the same as issue #1192, which was closed under the impression it was fixed with the latest beta (22). It still reproduces for me, but I have investigated it and am putting in a PR very soon.

Capacitor version: beta.22

node version: 10.13

npm version: 6.4

CocoaPods version: n/a

Steps to reproduce:

Send Firebase (FCM) message to Capacitor app not running in foreground. Tap on notification in Android’s notification panel.

Either nothing happens (app is not started or background app not brought to foreground), or the app is started up without sending the notification data to any of its listeners pushNotificationReceived or pushNotificationActionPerformed.

Link to sample project: n/a

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
vadimrostokcommented, Nov 23, 2020

We are moving to Capacitor and needed to support old and new systems, so notifications contained click_action: ST_ACTIVATE field. The solution was to add following intent-filter to main activity in AndroidManifest.xml:

<intent-filter>
  <action android:name="ST_ACTIVATE" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
1reaction
CFT-Chriscommented, May 7, 2019

Thanks for the quick reply.

I am testing on Android 5.0 (minimum required by Capacitor).

I tried all payload combinations. Without “notification”, then without “data”, with both “notification” and “data”, with “click_event” and without, etc.

Just an example of what I am sending (via Node.js backend POST request to Firebase cloud) which works with the changes I proposed in the PR:

request({
  url: 'https://fcm.googleapis.com/fcm/send',
  method: 'POST',
  headers: {
    'Content-Type' :' application/json',
    'Authorization': 'key=' + /* FCM key */
  },
  body: JSON.stringify({
    to: / *** PushReg Id ***/,
    data: { tm: Date.now() },
    notification: {
      sound: 'default',
      click_action: "CAPACITOR_FIREBASE_MESSAGING_ACTIVITY"
    }
  }
})

Without “notification” the notification doesn’t even arrive at all if the app is not running (background or killed).

Just looking at the code itself, I can’t figure out how Android ends up calling handleOnNewIntent in PushNotifications.java as there is nothing in the AndroidManifests, apart from CapacitorFirebaseMessagingService, that might deal with incoming Firebase messages when the app is not running in the foreground.

In my case, when the Capacitor app is in the background or not running, the Firebase message results in a notification in Android’s notification panel, and tapping that notification does not make CapacitorFirebaseMessagingService’s onMessageReceived handle it.

Could be old Android behaviour that doesn’t repro in later OS versions, but regardless, how would the code inside handleOnNewIntent (pushnotifications.java) get called, and how does any listener to pushNotificationActionPerformed get anything? Do later versions of Android somehow know to hook into the PushNotifications plugin and send an Intent there?

Sorry if I overlooked something obvious.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How exactly do apps not running "in the background" receive ...
It will always be there to receive a push notification. While you could circumvent it, it could mean that you could miss notifications...
Read more >
Android Notifications Not Showing Up? 10 Fixes You Can Try
Not seeing notifications show up on your Android phone? Try these fixes to get Android notifications working again.
Read more >
firebase - Clicking on notification doesn't open Android app
The code which I am using for opening app on the click of notification, which is working perfectly fine :
Read more >
Notifications Not Shown - Mobile Push
Android App is Force Stopped ; MIUI 10, Settings > Battery & performance > Choose apps > Select the app that is not...
Read more >
Push notification in Background wo… | Apple Developer Forums
When I tap the app and and the app received push notification in Background, that function is called in Active but not called...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found