[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 艾摩杰~

留言

這個網誌中的熱門文章

Synology NAS + PXE 掛載iSCSI 打造無硬碟系統 啟動WinXP,Win7

[PS3] PS3 最完整 改機 破解 自製固件 教學 (傻瓜教學版) (韌體Rogero_CEX_4.30_v2.05)

[NodeJS] Synology NAS 架設 NodeJS Server , 並用 forever 背景執行程式