I am getting familiar with sending notifications to devices,
I am currently trying to send a notification to my device using pubnub and Google cloud messaging:
(function() {
var pubnub = PUBNUB.init({
subscribe_key: 'my-subscribe',
publish_key: 'my-publish',
});
var os = 'android';
var regid = '';
var channel = 'mi-canal';
function sendPush() {
var gwtype = (os === 'ios') ? 'apns' : 'gcm';
console.log('sending push notification...',channel,gwtype);
pubnub.mobile_gw_provision({
device_id: 'APA91bE0rX8mfzY0VttACffUPIagSbd5No0xxxxxxxxxxxxxxxxxEHCVFBbzPqPHKG9phyQ31O4W6aEgMUqWANtWrK-zdMsiONUwAbUW4efso8ScemlXGM8tJq0M12oR0E2Kk9',
channel: channel,
op: 'add',
gw_type: gwtype,
error: function(msg){console.log(msg);},
callback: function() {
var message = PNmessage();
message.pubnub = pubnub;
message.callback = function (msg){ console.log(msg); };
message.error = function (msg){ console.log(msg); };
message.channel = channel;
message.apns = {
alert: 'The room temperature is set too high'
};
message.gcm = {
title: 'PubNub Push Demo',
message: 'The room temperature is set too high'
};
message.publish();
}
});
}
sendPush();
})();
Which logs:
[1, "Sent", "14471821928645156"]
the problem is that I don't receive the notification in the device, with and android app (cordova made)
var pushNotification = window.plugins.pushNotification;
//pushNotification.unregister(successHandler, errorHandler);
pushNotification.register(
successHandler,
errorHandler,
{
'senderID':'api-project-xxxxxxxx',
'ecb':'onNotificationGCM' // callback function
}
);
pusNotification.subscribe({
channel: 'mi-canal',
message: function(m){
alert('Conslog: '+m);
},
error: function (error) {
// Handle error here
console.log(JSON.stringify(error));
}
});
Any idea what I'm missing?
I personally use OneSignal on my android app and it works like a charm. You can read the documentation here. It's 100% free and also uses Google's cloud messaging service. Basically after installing their SDK and following the entire documentation, your code will be something like this.
Code example:
document.addEventListener('deviceready', function () {
// Enable to debug issues.
// window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});
var notificationOpenedCallback = function(jsonData) {
console.log('didReceiveRemoteNotificationCallBack: ' + JSON.stringify(jsonData));
};
window.plugins.OneSignal.init("b2f7f966-d8cc-11e4-bed1-df8f05be55ba",
{googleProjectNumber: "703322744261"},
notificationOpenedCallback);
// Show an alert box if a notification comes in when the user is in your app.
window.plugins.OneSignal.enableInAppAlertNotification(true);
}, false);
- Replace "b2f7f966-d8cc-11e4-bed1-df8f05be55ba" with your OneSignal App Id.
- Replace "703322744261" with your Google Project Number.
To me, as soon as I send a notification it takes exactly 3 seconds till my phone gets a beep. Hope this helps :)