2016-05-06 68 views
1

我想根據內部事件(例如,假設超時)從Ionic應用程序發送用戶推送通知,但我在網上找到的所有示例都告訴我如何獲取來自服務器的通知。沒有服務器的Ionic推送通知

任何人都可以幫忙嗎?

回答

3

當然,你要尋找的插件是http://ngcordova.com/docs/plugins/localNotification/

只需添加插件

cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git

,並看看這個例子:

module.controller('MyCtrl', 
 
    ['$scope', '$rootScope', '$ionicPlatform', '$cordovaLocalNotification', 
 
    function($scope, $rootScope, $ionicPlatform, $cordovaLocalNotification) { 
 
    
 
    $ionicPlatform.ready(function() { 
 
    
 
    // ========== Scheduling 
 
    
 
    $scope.scheduleSingleNotification = function() { 
 
     $cordovaLocalNotification.schedule({ 
 
     id: 1, 
 
     title: 'Title here', 
 
     text: 'Text here', 
 
     data: { 
 
      customProperty: 'custom value' 
 
     } 
 
     }).then(function (result) { 
 
     // ... 
 
     }); 
 
    }; 
 
    
 
    $scope.scheduleMultipleNotifications = function() { 
 
     $cordovaLocalNotification.schedule([ 
 
     { 
 
      id: 1, 
 
      title: 'Title 1 here', 
 
      text: 'Text 1 here', 
 
      data: { 
 
      customProperty: 'custom 1 value' 
 
      } 
 
     }, 
 
     { 
 
      id: 2, 
 
      title: 'Title 2 here', 
 
      text: 'Text 2 here', 
 
      data: { 
 
      customProperty: 'custom 2 value' 
 
      } 
 
     }, 
 
     { 
 
      id: 3, 
 
      title: 'Title 3 here', 
 
      text: 'Text 3 here', 
 
      data: { 
 
      customProperty: 'custom 3 value' 
 
      } 
 
     } 
 
     ]).then(function (result) { 
 
     // ... 
 
     }); 
 
    }; 
 
    
 
    $scope.scheduleDelayedNotification = function() { 
 
     var now = new Date().getTime(); 
 
     var _10SecondsFromNow = new Date(now + 10 * 1000); 
 
     
 
     $cordovaLocalNotification.schedule({ 
 
     id: 1, 
 
     title: 'Title here', 
 
     text: 'Text here', 
 
     at: _10SecondsFromNow 
 
     }).then(function (result) { 
 
     // ... 
 
     }); 
 
    }; 
 
    
 
    $scope.scheduleEveryMinuteNotification = function() { 
 
     $cordovaLocalNotification.schedule({ 
 
     id: 1, 
 
     title: 'Title here', 
 
     text: 'Text here', 
 
     every: 'minute' 
 
     }).then(function (result) { 
 
     // ... 
 
     }); 
 
    }; 
 
    
 
    // =========/ Scheduling 
 
    
 
    // ========== Update 
 
    
 
    $scope.updateSingleNotification = function() { 
 
     $cordovaLocalNotification.update({ 
 
     id: 1, 
 
     title: 'Title - UPDATED', 
 
     text: 'Text - UPDATED' 
 
     }).then(function (result) { 
 
     // ... 
 
     }); 
 
    }; 
 
    
 
    $scope.updateMultipleNotifications = function() { 
 
     $cordovaLocalNotification.update([ 
 
     { 
 
      id: 1, 
 
      title: 'Title 1 - UPDATED', 
 
      text: 'Text 1 - UPDATED' 
 
     }, 
 
     { 
 
      id: 2, 
 
      title: 'Title 2 - UPDATED', 
 
      text: 'Text 2 - UPDATED' 
 
     }, 
 
     { 
 
      id: 3, 
 
      title: 'Title 3 - UPDATED', 
 
      text: 'Text 3 - UPDATED' 
 
     } 
 
     ]).then(function (result) { 
 
     // ... 
 
     }); 
 
    }; 
 
    
 
    // =========/ Update 
 
    
 
    // ========== Cancelation 
 
    
 
    $scope.cancelSingleNotification = function() { 
 
     $cordovaLocalNotification.cancel(1).then(function (result) { 
 
     // ... 
 
     }); 
 
    }; 
 
    
 
    $scope.cancelMultipleNotifications = function() { 
 
     $cordovaLocalNotification.cancel([1, 2]).then(function (result) { 
 
     // ... 
 
     }); 
 
    }; 
 
    
 
    $scope.cancelAllNotifications = function() { 
 
     $cordovaLocalNotification.cancelAll().then(function (result) { 
 
     // ... 
 
     }); 
 
    }; 
 
    
 
    // =========/ Cancelation 
 
    
 
    // ========== Events 
 
    
 
    $rootScope.$on('$cordovaLocalNotification:schedule', 
 
    function (event, notification, state) { 
 
     // ... 
 
    }); 
 
    
 
    $rootScope.$on('$cordovaLocalNotification:trigger', 
 
    function (event, notification, state) { 
 
     // ... 
 
    }); 
 
    
 
    $rootScope.$on('$cordovaLocalNotification:update', 
 
    function (event, notification, state) { 
 
     // ... 
 
    }); 
 
    
 
    $rootScope.$on('$cordovaLocalNotification:clear', 
 
    function (event, notification, state) { 
 
     // ... 
 
    }); 
 
    
 
    $rootScope.$on('$cordovaLocalNotification:clearall', 
 
    function (event, state) { 
 
     // ... 
 
    }); 
 
    
 
    $rootScope.$on('$cordovaLocalNotification:cancel', 
 
    function (event, notification, state) { 
 
     // ... 
 
    }); 
 
    
 
    $rootScope.$on('$cordovaLocalNotification:cancelall', 
 
    function (event, state) { 
 
     // ... 
 
    }); 
 
    
 
    $rootScope.$on('$cordovaLocalNotification:click', 
 
    function (event, notification, state) { 
 
     // ... 
 
    }); 
 
    
 
    // =========/ Events 
 
    
 
    }); 
 
    
 
}]);

+0

謝謝!它啓動 –

+0

如何從webservice加載動態數據 –

+0

@ R.Anandan:到目前爲止您嘗試過了什麼? – Nikola