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

No sound and vibration for GCM when built with Cordova but Ok with PhoneBuild #520

Closed
gongfan99 opened this issue Jan 16, 2016 · 4 comments

Comments

@gongfan99
Copy link

My phone's android version: 5.0.1

Previously when I use Phonegap Build to create android GCM app, everything works fine. When app is in background, notification shows up in shade and emit sound/vibration. Then when I click the notification in shade, the app goes to foreground and "notification" event is fired. All good.

Then I download cordova (v 5.4.1) and android SDK ("android-22") to build the same app locally. The app is successfully built. When app is in background, notification shows up in shade but does NOT emit sound/vibration. When I click the notification in shade, the app goes to foreground but "notification" event is NOT fired.

The app's index.html is:

<!DOCTYPE html>
<html>
<meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline'">
<body>
  <p id="debuginfo">Debug: ...</p>
  <script src="cordova.js"></script>
  <script>
if (window.cordova) {
    document.addEventListener('deviceready', function () {
        var myConsoleLog = (function(){ // show messages in "debuginfo" DOM element
            var debuginfo = '';
            return function(msg){
                debuginfo = debuginfo + '<br></br> --- ' + msg;
                document.getElementById("debuginfo").innerHTML = debuginfo;
            }
        })();
        var push = PushNotification.init({
                android: {
                        senderID: "6xxxxxxxxxxx0"
                },
                ios: {
                        alert: "true",
                        badge: "true",
                        sound: "true"
                },
                windows: {}
        });
        push.on('registration', function(data) {
            // data.registrationId
            console.log(data.registrationId);
            myConsoleLog(data.registrationId);
        });
        push.on('notification', function(data) {
            console.log(JSON.stringify(data.additionalData));
            myConsoleLog(JSON.stringify(data.additionalData));
        });
        push.on('error', function(e) {
            // e.message
            console.log(JSON.stringify(e.message));
            myConsoleLog(JSON.stringify(e.message));
        });
    })
}
  </script>
</body>
</html>

The config.xml is:

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.sample.MyApp2" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>MyApp2</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="[email protected]" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <preference name="android-build-tool" value="gradle" />
    <icon src="icon.png" />
    <splash src="splash.png" />
    <plugin name="cordova-plugin-vibration" />
    <plugin name="cordova-plugin-splashscreen" />
    <plugin name="cordova-plugin-media" />
    <plugin name="phonegap-plugin-push" spec="1.5.1" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

The GCM payload is:

{
    "to" : 'xxxxxxxxxxxxxx',
    "notification" :
    {
        "body" : "great match!",
        "title" : "Notification: Portugal vs. Denmark"
    },
    "data" :
    {
        "whatData": [{name: 'messages'}],
        "sound": "default"
    },
}

The only difference is "xmlns:" header in the config.xml. For Phonegap Build, it is "gap=http://phonegap.com/ns/1.0" while for cordova it is "cdv=http://cordova.apache.org/ns/1.0". The difference is not substantial, is it?

I am speculating that my cordova environment may be different from what Phonegap Build uses. I installed cordova and android SDK yesterday so each is the latest version.

When I look at the build log, some difference exists on some libraries' version.

Phonegap Build:

:prepareComAndroidSupportSupportV132310Library
:prepareComAndroidSupportSupportV42310Library
:prepareComGoogleAndroidGmsPlayServicesBase810Library
:prepareComGoogleAndroidGmsPlayServicesBasement810Library
:prepareComGoogleAndroidGmsPlayServicesGcm810Library

My local cordova:

:prepareComAndroidSupportSupportV132311Library UP-TO-DATE
:prepareComAndroidSupportSupportV42311Library UP-TO-DATE
:prepareComGoogleAndroidGmsPlayServicesBase840Library UP-TO-DATE
:prepareComGoogleAndroidGmsPlayServicesBasement840Library UP-TO-DATE
:prepareComGoogleAndroidGmsPlayServicesGcm840Library UP-TO-DATE
:prepareComGoogleAndroidGmsPlayServicesMeasurement840Library UP-TO-DATE

Where else should I start to check? Thank you very much.

@macdonst
Copy link
Member

@gongfan99 try sending this as your payload and let me know if that fixes it:

{
    "to" : 'xxxxxxxxxxxxxx',
    "data" :
    {
        "body" : "great match!",
        "title" : "Notification: Portugal vs. Denmark",
        "whatData": [{name: 'messages'}],
        "sound": "default"
    },
}

@gongfan99
Copy link
Author

WOW! It does fix it. Thank you so much.

I am just curious why? The phonegap build does not require this payload format.

@fredgalvao
Copy link
Collaborator

This is a duplicate of #515. You'll find more details on what's up with data|notification on payloads there.

@lock
Copy link

lock bot commented Jun 5, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 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

3 participants