2016-06-27 151 views
0

我想发送当地通知每天早上7点。我已经把在控制器下面的代码,发送本地推送通知与科尔多瓦

代码

function send_push_notification(){ 
    cordova.plugins.notification.local.schedule({ 
       id: 10, 
       title: "Report", 
       text: "Pls send a report :-)", 
       firstAt: alarm_time, 
       at: at_8_am, 
       every: "day" 
    }).then(function (success) { 
       return true; 
    }, function (err) { 
       return false 
    }); 
    } 

但它显示ReferenceError: cordova is not defined..我在第一个在我的应用程序的index.html文件中定义

<script src="cordova.js"></script>

我也试过这个http://ngcordova.com/docs/plugins/localNotification/链接中给出的例子。但不知道要遵循哪一个。两者完全不同。

更新:只有

cordova.plugins.notification.local.schedule方法的工作中deviceready事件侦听器,而不是在控制器中。我应该使它在控制器上工作..

即,我有一个任务发送本地推送通知,当没有数据库更新为该特定日期作出,否则不需要通知。

+0

尝试安装这样的科尔多瓦https://www.npmjs.com/package/cordova –

+0

同样的错误出现..我的app.js文件包含行'cordova.plugins',但控制器只显示错误..想想我需要在控制器中注入某些东西。 –

+0

你在模块数组中注入了ngco​​rdova –

回答

0

正如我在源建议,以下命令可以用来有分叉和修改push插件在项目中的工作:

cordova plugin add https://github.com/zxshinxz/PushPlugin.git 

默认的push插件需要被删除,这将是其替代。如果我正确理解了下面的帖子,该解决方案还可以在程序关闭时修复本地通知使用情况。

关于该链接文章的更多信息。

我来源:

Cordova local notification doesn't work while app is in background

0

这里是

步骤上科尔多瓦运行的本地通知的步骤:1 键入cmd提示你文件夹的

bower install ngCordova 

包括在主index.html文件之前cordova

<script src="lib/ngCordova/dist/ng-cordova.js"></script> 
<script src="cordova.js"></script> 

第2步:

进样依赖

angular.module('myApp', ['ngCordova']) 

安装这个插件

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

步骤:3

这里是在您的控制器

(function(){ 
     'use strict'; 

     angular.module('myApp').controller('Schedule',['$scope','$ionicPlatform','$rootScope','$cordovaLocalNotification','$cordovaSms',Schedule]); 


    function Schedule($scope,$ionicPlatform,$rootScope,$cordovaLocalNotification,$cordovaSms){  
      //************************Setting Notification*********************** 

    $ionicPlatform.ready(function() { 

      var now = new Date().getTime(); 
      var _10SecondsFromNow = new Date(now + 10 * 1000); 

      $cordovaLocalNotification.schedule({ 
      id: 10, 
      title: 'Report', 
      text: 'Text here', 
      at: _10SecondsFromNow 
      }).then(function (result) { 
      // ... 
      }); 
     }; 

     $cordovaLocalNotification.add({ 
      id: 10, 
      title: 'Report', 
      text: 'Pls send a report :-)', 
      firstAt: at_8_am, 
      every: 'day' 
      }).then(function (result) { 
      // ... 
      }); 

      $scope.scheduleSingleNotification = function() { 
      $cordovaLocalNotification.schedule({ 
      id: 1, 
      title: 'Title here', 
      text: 'Text here', 

      }).then(function (result) { 
      // ... 
      }); 
     }; 

}); 


//*******************Notification Ended*********************** 


})(); 
1

使用schedule.js本地通知文件下面的示例代码应该让你在发送在Android和iOS设备本地通知开始。

的index.html

<!DOCTYPE html> 
<html> 
    <head>   
     <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *"> 
     <meta name="format-detection" content="telephone=no"> 
     <meta name="msapplication-tap-highlight" content="no"> 
     <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> 
     <link rel="stylesheet" type="text/css" href="css/index.css"> 
     <title>Hello World</title> 
    </head> 
    <body> 
     <h3>Local Notification</h3> 
     <script type="text/javascript" src="js/jquery.js"></script> 
     <script type="text/javascript" src="cordova.js"></script> 
     <script type="text/javascript" src="js/index.js"></script> 
    </body> 
</html> 

index.js

$(document).ready(function() { 
    document.addEventListener("deviceready", onDeviceReady, false); 
}); 

function onDeviceReady() { 
    try { 
     cordova.plugins.notification.local.schedule({ 
      text: "This is the text.", 
      at: new Date(new Date().getTime() + 10000) 
     }); 
    } catch (e) { 
     alert("Fail " + e); 
    } 
} 

以下示例代码启动应用后发出对第十第二测试通知。该代码在Android和iOS设备上进行测试。以上示例代码可在github page中找到。您可以下载示例应用程序,安装通知插件并对其进行测试。

+0

不,他们给予宽限期的问题。 –

+0

@AvinashRaj做相同的测试。希望它有帮助 – Gandhi

+0

我在app.js文件中有相同的代码,它的工作原理..我可以包含相同的controller.js?那是另一个'$(document).ready(function(){'controller.js需要安排通知吗? –

相关问题