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

plugin doesn't work in background or when app not running on kitkat 4.4.2 #754

Closed
peterturquoise opened this issue Mar 27, 2016 · 10 comments

Comments

@peterturquoise
Copy link

Expected behabiour: I expect that a notification should be shown when the app using the plugin is not running and when it is running in the background

Actual behaviour: I can send a push message when the app is in the foreground and it works OK, but when the app is not running or is in the background, the push message is not shown.

The push message is simple and has the format {"data":{"message":"some message"}}

Any ideas ? I am using plugin version 1.6.1. The previous plugin version I used was 1.5.3 and I had the same issue with notifications not appearing in background or when the app wan't running but working fine with the app in foreground.

All works great in iOS. The app is built with cordova (via ionic) 6.0.0.

Reproduce Scenario (including but not limited to): Build an app in cordova 6.000 with the plugin, run the app on kitkat 4.4.2 and send it a push notification. The notification will display when the app is running in the foreground, but will not display of the app is in the background or if the app is not running.

Steps to Reproduce

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

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

Cordova CLI version and cordova platform version

cordova --version                                     6.0.0
cordova platform version android              android 5.1.1

Plugin version

cordova plugin version | grep phonegap-plugin-push  1.6.1

Sample Push Data Payload: {"data":{"message":"dd"}}

Sample Code that illustrates the problem

Logs taken while reproducing problem: I can take logs if that would help - what logs do you need and how can I get them?

@ansaritameem
Copy link

Add a title attribute to your payload, rest should work.
ex.
{
"data": {
"title" : "Your app title",
"message" : "dd"
}
}

@peterturquoise
Copy link
Author

Thanks ansaritameem, but the problem turned out to be it was just a little bit too late in the evening....

As soon as I looked at it next day, and looked at the docs again, I realised that I was missing the crucial

"content-available":"1"

data in the payload

(See https://github.com/phonegap/phonegap-plugin push/blob/master/docs/PAYLOAD.md#background-notifications - "Background notifications" section)

So all I needed in the payload was

{
"data":
{
"message":"$message>",
"content-available":"1"
}
}

And it started working.

At this point foreground, background and alerts when the app wasn't running were all successfully delivered.

To be clear, I didn't need the "Title" - but a good suggestion.

Thanks to Simon for a very useful plugin.

@ansaritameem
Copy link

@peterturquoise,Thanks for sharing your feedback... I should have studied this better. I found that setting the title was more than sufficient for Android to deliver notifications on foreground & background. I am using Azure Notification Hubs and I am now wondering if it pads up anything to the payload or not. What are you using to push the notification to the devices? Do you find any difference between setting the title w/o the content-available attribute and vice versa or both?

@macdonst
Copy link
Member

@ansaritameem & @peterturquoise: If you set the title or message/body parameter in the data object of your push payload you should get a notification in the notification shade area of your Android device if the app is in the background or stopped. If you want to see the notification in the foreground as well then you set the forceShow parameter to true.

The on('notification') handler will be called immediately if your app is in the foreground or immediately after the user clicks on the notifications shown in the notification shade.

If you want the on('notification') handler called when the app is in the background then you set the "content-available":"1" property in the data object of your payload.

@samuelcastro
Copy link

Even with "content-available":"1" is not working for me, I'm testing a Samsung 4.1.2 GT-l8552B, I can see the notification arriving using adb logcat but no alert, no vibration, no message if the app is in background.

@macdonst
Copy link
Member

@samuelcastro what does you payload look like?

@samuelcastro
Copy link

@macdonst see below:

message.addData('content-available', '1');
message.addData('title','Test' );
message.addData('message',"This is my test message");
message.addData('vibrationPattern', [2000, 1000, 500, 500]);
message.addData('priority', 2);
message.addData('soundname', 'default');

@macdonst
Copy link
Member

@samuelcastro would like to see you logcat when reproducing the bug, adb logcat | grep PushPlugin.

@samuelcastro
Copy link

@macdonst See below:

Samuels-MacBook-Pro:mobile samuelcastro$ adb logcat | grep PushPlugin
D/PushPlugin_GCMIntentService(20781): onMessage - from: 738521293070
D/PushPlugin_GCMIntentService(20781): normalize extras
D/PushPlugin_GCMIntentService(20781): key = vibrationPattern
D/PushPlugin_GCMIntentService(20781): replace key vibrationPattern with vibrationPattern
D/PushPlugin_GCMIntentService(20781): key = message
D/PushPlugin_GCMIntentService(20781): replace key message with message
D/PushPlugin_GCMIntentService(20781): key = title
D/PushPlugin_GCMIntentService(20781): replace key title with title
D/PushPlugin_GCMIntentService(20781): key = collapse_key
D/PushPlugin_GCMIntentService(20781): replace key collapse_key with collapse_key
D/PushPlugin_GCMIntentService(20781): key = soundname
D/PushPlugin_GCMIntentService(20781): replace key soundname with sound
D/PushPlugin_GCMIntentService(20781): key = content-available
D/PushPlugin_GCMIntentService(20781): replace key content-available with content-available
D/PushPlugin_GCMIntentService(20781): key = priority
D/PushPlugin_GCMIntentService(20781): replace key priority with priority
D/PushPlugin_GCMIntentService(20781): background
D/PushPlugin_GCMIntentService(20781): message =[This is my test message]
D/PushPlugin_GCMIntentService(20781): title =[Test]
D/PushPlugin_GCMIntentService(20781): contentAvailable =[1]
D/PushPlugin_GCMIntentService(20781): create notification
E/PushPlugin_GCMIntentService(20781): Number format exception - Error parsing notId: Invalid int: "null"
D/PushPlugin_GCMIntentService(20781): stored icon=null
D/PushPlugin_GCMIntentService(20781): stored iconColor=null
D/PushPlugin_GCMIntentService(20781): stored sound=true
D/PushPlugin_GCMIntentService(20781): stored vibrate=true
D/PushPlugin_GCMIntentService(20781): no icon resource found - using application icon
D/PushPlugin_GCMIntentService(20781): create actions
D/PushPlugin_GCMIntentService(20781): send notification event
D/PushPlugin(20781): convert extras to json
D/PushPlugin(20781): key = message
D/PushPlugin(20781): key = vibrationPattern
D/PushPlugin(20781): key = title
D/PushPlugin(20781): key = collapse_key
D/PushPlugin(20781): key = content-available
D/PushPlugin(20781): key = priority
D/PushPlugin(20781): key = sound
D/PushPlugin(20781): key = foreground
V/PushPlugin(20781): extrasToJSON: {"message":"This is my test message","additionalData":{"vibrationPattern":[2000,1000,500,500],"collapse_key":"do_not_collapse","content-available":"1","foreground":false,"priority":"2"},"sound":"default","title":"Test"}

@lock
Copy link

lock bot commented Jun 4, 2018

This thread has been automatically locked.

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

No branches or pull requests

4 participants