[ionic] cordovaPush GCM 推播 範例
安裝 PushPlugin
cordova plugin add https://github.com/phonegap-build/PushPlugin.git
app.js
angular.module('app', ['ionic','ngCordova'])
.run(function($ionicPlatform,$cordovaPush,$rootScope) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
var androidConfig = {
"senderID": "XXXXXXXXXX",
};
$cordovaPush.register(androidConfig).then(function(result) {
//alert(result);
}, function(err) {
alert(err);
})
});
$rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) {
switch(notification.event) {
case 'registered':
if (notification.regid.length > 0 ) {
alert('registration ID = ' + notification.regid);
console.log('registration ID = ' + notification.regid);
}
break;
case 'message':
alert(JSON.stringify(notification));
break;
default:
alert('An unknown GCM event has occurred:'+notification.event);
break;
}
});
})
資料來源:http://aimojay.blogspot.tw/2016/02/ionic-cordovapush-gcm.html
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。