[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;
}
});
})
有任何疑問歡迎留言
By 艾摩杰~
留言
張貼留言