0

I'm trying to send local notifications for every day at 7 am. I have placed the below code in controller,

Code

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
     });
  }

But it shows ReferenceError: cordova is not defined.. I have defined

<script src="cordova.js"></script> at very first in my app's index.html file.

I also tried the example given in this http://ngcordova.com/docs/plugins/localNotification/ link. But donno which one to follow. Both are totally different.

Update:

cordova.plugins.notification.local.schedule method only works inside deviceready event listener but not in the controller. I should make it to work on controller..

ie, I have a task of sending local push notification when there is no database update made for that particular date else no need for notification.

I'm Geeker
  • 4,601
  • 5
  • 22
  • 41
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • try installing cordova like this https://www.npmjs.com/package/cordova – Mohan Gopi Jun 27 '16 at 12:43
  • same error appears.. My app.js file contain lines like `cordova.plugins` but the controller only shows error.. Think I need to inject something in the controller. – Avinash Raj Jun 27 '16 at 12:47
  • have you injected ngcordova inside your module array – Mohan Gopi Jun 27 '16 at 13:02
  • ya mu module line looks like `angular.module('starter', ['ionic', 'ngCordova', 'starter.controllers', 'starter.services', 'starter.filters', 'starter.directives'])` – Avinash Raj Jun 27 '16 at 13:04
  • @AvinashRaj If you are using Ionic framework, you gotta ensure that plugin code is invoked inside platform ready function or it should be called after firing platform ready event. In case of vanilla cordova app, you gotta invoke plugin code inside document ready event listener which ensures that cordova is loaded completely. To make it work on controller, ensure that the code executed only after device ready event it fired. – Gandhi Jun 30 '16 at 09:26
  • @AvinashRaj Request you to look try out this simple notification app on my github page - https://github.com/gandhirajan/Cordova_Local_Notification its tested and works on android and ios device – Gandhi Jun 30 '16 at 10:31
  • @Gandhi if we shedule the notification for every day on particular time, does the notification will appear even if the app is in closed state? – Avinash Raj Jun 30 '16 at 11:49
  • And also I have another doubt.., If need to schedule a notification when `$scope.out` is true , how the code looks like for that situation? Whenever I add `cordova.plugins.notification.local.schedule` On my controller, it throws `plugins` not defined error. It works only on my `app.js` and inside the `deviceready` event listener. – Avinash Raj Jun 30 '16 at 11:54
  • @AvinashRaj Check this Avinash - http://ngcordova.com/docs/common-issues/ Thats the general rule for plugins. Otherwise you cannot be sure whether it is loaded completely Official docs to says the same – Gandhi Jun 30 '16 at 11:56
  • does it work if I modify the code like `if($scope.out===true){ document.addEventListener("deviceready", function () { $cordovaPlugin.someFunction().then(success, error); }, false); }` – Avinash Raj Jun 30 '16 at 11:59
  • @AvinashRaj One way to get it work in controller is to bootstrap angular module once cordova is loaded. Check out ptrpt's comment in this link - https://github.com/cordova-plugin-camera-preview/cordova-plugin-camera-preview/issues/96 – Gandhi Jun 30 '16 at 12:06
  • @AvinashRaj The lines you mentioned should work as any plugin code wrapped inside device ready event should work fine. – Gandhi Jun 30 '16 at 12:07
  • ok, I should try and let you know.. Thanks for your efforts.. – Avinash Raj Jun 30 '16 at 12:18
  • @AvinashRaj And to answer your question on daily notification, as fas as i know, the app should be running in background mode to get notification. It may not work in closed state – Gandhi Jun 30 '16 at 12:35
  • @AvinashRaj Any update on this? – Gandhi Jul 01 '16 at 12:17
  • @Gandhi my volume icon shows per each 5 mins.. Any way to stop this? – Avinash Raj Jul 01 '16 at 13:16
  • @AvinashRaj could you post the code you are trying? – Gandhi Jul 01 '16 at 13:46
  • @AvinashRaj did you tried out the local notification sample code I sent? – Gandhi Jul 03 '16 at 14:35
  • @Gandhi I didn't tested yet. Are you sure that the above code would work? If yes, then make it as an answer. – Avinash Raj Jul 07 '16 at 05:00
  • @AvinashRaj Have tested the code successfully in android device running in android marshmallow and iOS device running in version 9.2.1 You can download the code from github link - https://github.com/gandhirajan/Cordova_Local_Notification, install notification and test the same. Will post the code in answer too. Thanks for the response – Gandhi Jul 07 '16 at 05:27
  • pls do that.. Bounty going to expire in 5 mins. – Avinash Raj Jul 07 '16 at 05:29

3 Answers3

2

The following sample code should get you started in sending local notifications in Android and iOS device.

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);
    }
}

The following sample code sends out a test notification on 10th second after launching the app. The code is tested in Android and iOS devices. The above sample code is available in the github page. You can download the sample app, install notification plugin and test the same.

Gandhi
  • 11,875
  • 4
  • 39
  • 63
  • @AvinashRaj Do test the same. Hope it helps – Gandhi Jul 07 '16 at 05:37
  • I have the same code in app.js file and it works.. May I include the same in controller.js? Is that another `$(document).ready(function() {` required on controller.js to schedule a notification? – Avinash Raj Jul 07 '16 at 05:38
  • @AvinashRaj Great. You can extend the same to your requirement. You can do the needful for the answer if it was helpful – Gandhi Jul 07 '16 at 05:39
  • @AvinashRaj You can provided deviceready event is fired prior to this – Gandhi Jul 07 '16 at 05:40
  • So I have to check whether the deviceready event is fired or not. If it's fired then we are free to use `cordova.plugin.notification` code , right? – Avinash Raj Jul 07 '16 at 05:42
  • @AvinashRaj You are absolutely rite.. This event ensures that cordova and plugins are completely loaded and its free to use – Gandhi Jul 07 '16 at 09:07
  • @AvinashRaj Hope it was useful. Please do the needful – Gandhi Jul 07 '16 at 11:24
  • offered the bounty but I need some info.. I want to check a condition before scheduling the event.. like, `if($scope.value == true) {cordova.notif.local}` . Is this possible? – Avinash Raj Jul 07 '16 at 12:59
  • @Avinash ideally it should be possible.not tried though as I dont use angular in my app – Gandhi Jul 08 '16 at 01:30
0

As suggested in my source, the following command could be used to have a forked and modified push plugin to work in your project:

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

The default push plugin needs to be removed and this will be its replacement. If I understood correctly the post below, this solution fixes local notification usage also when the program is turned off.

More info on that linked post.

My source:

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

Community
  • 1
  • 1
mico
  • 12,730
  • 12
  • 59
  • 99
0

Here is a steps for running local notifications on cordova

Step : 1 Type in cmd prompt of your folder

bower install ngCordova

include in your main index.html file before cordova

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

Step:2

Inject dependency

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

Install this plugin

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

Step:3

Here is schedule.js local notification file used in your controller

    (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***********************


})();
Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29