eroticid
eroticid

Reputation: 39

How to get GCM ID using phonegap-plugin-push

I am trying to get GCM ID with the latest version of phonegap-plugin-push.

I have built many sample code from the internet but none of them are working.

I have followed the steps from phonegaptut.com, and phonegappro.com, and this phonegap-plugin-push example too.

I have followed the instructions carefully but the GCM ID is not there.

These are the steps I have taken:

<html>
<head>
    <title>PhonegapTut</title>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="jquery.js"></script>
    <style> textarea { width:100%; } </style>    
    <script type="text/javascript">    
        document.addEventListener("deviceready",onDeviceReady,false);

        function onDeviceReady(){    
            console.log("Device Ready");    
            var push = PushNotification.init({ "android": {"senderID": "MY SENDER ID"},"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );

            push.on('registration', function(data) {    
                console.log(data.registrationId);    
                $("#gcm_id").val(data.registrationId);
            });    

            push.on('notification', function(data) {    
                console.log(data.message);    
                alert(data.title+" Message: " +data.message);    
                // data.title,    
                // data.count,    
                // data.sound,    
                // data.image,    
                // data.additionalData    
            });   

            push.on('error', function(e) {
                console.log(e.message);    
            });    
        }

    </script>
</head>
<body>
    <h2>Push Notification Example</h2>
    Your GCM ID : <br/>
    <textarea id="gcm_id" rows="10"></textarea>
</body>
</html>

However, the GCM ID does not appear; can anyone see anything wrong with the code?

Upvotes: 1

Views: 1382

Answers (1)

eroticid
eroticid

Reputation: 39

Here's my solution

  • phonegap create my-app --template phonegap-template-push
  • phonegap platform add android
  • edit and replace SENDER_ID "XXXXXXX" with your GCM project number in config.xml

Blockquote

<plugin name="phonegap-plugin-push" spec="1.6.0">
<param name="SENDER_ID" value="XXXXXXX" />
</plugin>
  • edit and replace SENDER_ID "XXXXXXX" with your GCM project number in js/index.js file

Blockquote

var push = PushNotification.init({
android: {
senderID: "XXXXXXX"
},
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
},
ios: {
alert: "true",
badge: "true",
sound: "true"
},
windows: {}
});'
  • add a new code to set registration id

    console.log('registration event: ' + data.registrationId); document.getElementById("gcm_id").innerHTML = data.registrationId;

  • add < div > in your index.html to display GCM ID

Blockquote

<div id="gcm_id"> registration id: </div>
  • phonegap build android
  • phonegap run android --device

and your GCM ID will be displayed inside the DIV

Upvotes: 1

Related Questions