Skip to content

Instantly share code, notes, and snippets.

@macdonst
Created January 13, 2016 20:47
Show Gist options
  • Save macdonst/191f74ac75b6802c047d to your computer and use it in GitHub Desktop.
Save macdonst/191f74ac75b6802c047d to your computer and use it in GitHub Desktop.
var app = {
// Application Constructor
initialize: function() {
this.initPush();
this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicitly call 'app.receivedEvent(...);'
onDeviceReady: function() {
console.log('device is ready');
},
initPush: function() {
var push = PushNotification.init({
"android": {
"senderID": "1234567890"
},
"ios": {"alert": "true", "badge": "true", "sound": "true"},
"windows": {}
});
push.on('registration', function(data) {
console.log("registration event");
document.getElementById("regId").innerHTML = data.registrationId;
console.log(JSON.stringify(data));
});
push.on('notification', function(data) {
console.log("notification event");
console.log(JSON.stringify(data));
var cards = document.getElementById("cards");
var card = '<div class="row">' +
'<div class="col s12 m6">' +
' <div class="card darken-1">' +
' <div class="card-content black-text">' +
' <span class="card-title black-text">' + data.title + '</span>' +
' <p>' + data.message + '</p>' +
' </div>' +
' </div>' +
' </div>' +
'</div>';
cards.innerHTML += card;
push.finish(function () {
console.log('finish successfully called');
});
});
push.on('error', function(e) {
console.log("push error");
});
}
};
app.initialize();
@mayur8188
Copy link

mayur8188 commented May 21, 2016

I have complete following procedure:

  1. added above code in index.js file
  2. add this line in in config.xml
  3. add this line "<script type="text/javascript" src="cordova.js"></script>" in index.html

but this function "push.on('registration', function(data) {" never get call back and never get registrationId. :(

someone please help me here.I am using "build.phonegap.com" to build project.

I want to setup this for IOS only.

thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment