Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Android - app doesn't come to foreground when clicking notification #1819

Closed
szh opened this issue Jun 30, 2017 · 15 comments
Closed

Android - app doesn't come to foreground when clicking notification #1819

szh opened this issue Jun 30, 2017 · 15 comments

Comments

@szh
Copy link
Contributor

szh commented Jun 30, 2017

Expected Behaviour

When clicking a data notification while the app is in the background (but still in memory), the app should launch.

Actual Behaviour

The on('notification') handler is called, but the app stays in the background and doesn't appear on the screen.

Reproduce Scenario (including but not limited to)

Steps to Reproduce

Send a notification that contains a title and body as well as data key-value pairs, following the recommendation of the docs to put all the values in the 'data' object (see below for sample payload).
When the app is in the foreground, the on('notification') listener is called and the notification is not shown in the system tray, as expected.
When the app is no longer in memory, the notification shows in the system tray, and when clicked, the app opens and the on('notification') listener is called, as expected.
However, when the app is in the background but still in memory, the notification is shown in the system tray as expected, but when clicking the notification, only the on('notification') listener is called, but the app doesn't come to the foreground.

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 6.0

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

Motorola Moto X 2nd Generation

Cordova CLI version and cordova platform version

cordova --version

7.0.1

cordova platform version android

6.3.2

Plugin version

cordova plugin version | grep phonegap-plugin-push

2.0.0-rc5

Sample Push Data Payload

{
  "registration_ids": [
    "...", "..."
  ],
  "collapse_key": "Lorem ipsum",
  "priority": "normal",
  "content_available": true, /* Same results if using 'content-available': 1 in the 'data' object */
  "restricted_package_name": "com.myapp",
  "data": {
    "title": "Notification title",
    "body": "Notification text",
    "badge": 10,
    "Foo": "bar",
    "somekey": "somevalue"
  }
}

Sample Code that illustrates the problem

var push = PushNotification.init({
  android: { senderID: "..." },
  ios: {
	alert: true,
	badge: true,
	sound: true
  }
});

push.on("notification", (data) => {
  console.log("Notification received:");
  console.log(data);
})
@MarsupiL
Copy link

MarsupiL commented Jul 6, 2017

I'm experiencing the same issue with a payload with data only, with or without content_available, under
cordova 6.5.0
cordova android 6.2.2
phonegap-plugin-push 1.10.5
The on('notification') handler is called but the app stays in the background when the notification is tapped.

@acss1881
Copy link

Help please, me too.

ionic = 3.0.0-rc.2
cordova 7.0.1
phonegap-plugin-push = 1.10.5

@malwatte
Copy link
Contributor

I had this kind of a issue sometime ago. Problem was my payload was wrong. I was using the same payload for both Android and iOS. And found that Android payload should be different.

something like this,
{
"data": {
"body" : "BODY",
"title" : "TITLE",
"data1": "DATA",
"notId": 1
},
"priority":"high",
"registration_ids" :[{{device_token array}}]
}

(I don't know if it is the same issue)

@acss1881
Copy link

not my friend.
thank you

@szh
Copy link
Contributor Author

szh commented Jul 31, 2017

This is a pretty serious issue. Any plans to fix it?

@acss1881
Copy link

@macdonst
Copy link
Member

@szh I can't reproduce this issue. What you are doing looks correct but one quick clarifying question. What is the android:launchMode set in your activity in the application tag in AndroidManifest.xml?

@szh
Copy link
Contributor Author

szh commented Aug 22, 2017

@macdonst android:launchMode="singleInstance"

@MarsupiL
Copy link

same

@macdonst
Copy link
Member

@szh and @MarsupiL whelp a quick search for singleInstance in this repo gives us 11 results and probably my best explanation of it is in #1690.

This is actually the behaviour I would expect from an app set to singleInstance. That particular launch mode should only be used for launcher type apps so I'm not sure why you'd be using it. Regardless, this has come up way too often and I'll add it to the docs.

@szh
Copy link
Contributor Author

szh commented Aug 23, 2017

@macdonst Thanks, it works now!

@szh szh closed this as completed Aug 23, 2017
@macdonst macdonst reopened this Aug 23, 2017
@macdonst
Copy link
Member

@szh I'm leaving this open until I update the docs

@MarsupiL
Copy link

MarsupiL commented Aug 24, 2017

Thanks @macdonst. I had indeed set up the lauchMode to singleInstance based on the recommendation from
https://github.com/nordnet/cordova-universal-links-plugin#prevent-android-from-creating-multiple-app-instances
in order to avoid multiple instances of the app when using deep links.
That's also probably why others out there did the same.
But it seems from the Android doc that singleTop should be sufficient.

@fredgalvao
Copy link
Collaborator

@MarsupiL as you probably already saw on the docs, and by interpreting the name, singleTop doesn't allow for an activity to be duplicated, and if an interaction with your MainActivity on cordova is requested, it'll simply fire a onNewIntent event on it. This only applies 100% to cordova, as in our case there's only one activity at max, all the time, at all times. Beware though, if your app includes any plugin or custom code that could create a new activity and have it stay at the top of the stack long enough, you could tecnically fall into the duplicate webview scenario.

tldr: singleTop will always guarantee a single instance of an activity, as long as it's the one at the top of the stack of the current task.

@lock
Copy link

lock bot commented Jun 3, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

6 participants