Will
Will

Reputation: 4715

Local Notifications with IonicFramework

I have a form where a user can tick which days and what time to set off an alarm. The form saves it's settings.

I want to now add a local notifications to fire as per the settings. I have used the ngCordova plugin to do this. The code does not raise an error, but I can't get a notification to fire.

This is what my controller looks like.

angular.module('starter.controllers', [])
.controller('DailyCtrl', function($scope, $location, Settings,  $cordovaLocalNotification) {
  $scope.data = {};
  $scope.data.settings = Settings.all();
  $scope.doSave = function() {
      Settings.save($scope.data.settings);
      var today = new Date();
      //save notification
       $scope.addNotification = function () {            
        $ionicPlatform.ready(function() {
          $cordovaLocalNotification.add({ message: 'Great app!' });
          $cordovaLocalNotification.add({
            id: 'happi_alert',                
            date:       new Date(today.getFullYear(), today.getMonth(), today.getDate(), 16, 33, 0),    
            message:    "Happi App Message",  // The message that is displayed
            title:      "HappiApp Alert",  // The title of the message          
            sound:      "beep.caf",  // plays `beep.mp3` located in folder 
            //json:       String,  // Data to be passed through the notification
            repeat:     "minutely",  
            autoCancel: true,          
          }).then(function () {
            console.log('callback for adding background notification');
          }); 
        });  
        $cordovaLocalNotification.getScheduledIds(function (scheduledIds) {
          alert('Scheduled IDs: ' + scheduledIds.join(' ,'));
        }, scope);              
      };
      $location.path("/");
  };
})

How can i start to debug this? Where might I be going wrong?

I have tried running the immediate notification directly in a controller like this

.controller('AboutCtrl', function($scope,  $cordovaLocalNotification) {
  $cordovaLocalNotification.add({ message: 'Great app!' });
})

However that crashes the emulator app with the following stack trace

TypeError: Cannot read property 'notification' of undefined
    at Object.add (ng-cordova.js:2979)
    at new <anonymous> (controllers.js:43)
    at invoke (ionic.bundle.js:11591)
    at Object.instantiate (ionic.bundle.js:11602)
    at $get (ionic.bundle.js:14906)
    at ionic.bundle.js:14295
    at forEach (ionic.bundle.js:7957)
    at nodeLinkFn (ionic.bundle.js:14282)
    at compositeLinkFn (ionic.bundle.js:13730)
    at nodeLinkFn (ionic.bundle.js:14330)

UPDATE

i have tried adding the notification directly using the window.plugin like this:

 window.plugin.notification.local.add({ 
      id: 'MYLN',     
      message: "this is a notification"
    });

but i get an error:

2014-12-29 12:31:40.051 HappiCards[32779:1207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString stringValue]: unrecognized selector sent to instance 0x7899dd00'
*** First throw call stack:
(
    0   CoreFoundation                      0x001e11e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x020998e5 objc_exception_throw + 44
    2   CoreFoundation                      0x0027e243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x001d150b ___forwarding___ + 1019
    4   CoreFoundation                      0x001d10ee _CF_forwarding_prep_0 + 14
    5   HappiCards                          0x000f7387 -[APPLocalNotification notificationWithId:] + 503
    6   HappiCards                          0x000f7116 -[APPLocalNotification isNotificationScheduledWithId:] + 86
    7   HappiCards                          0x000f373f __28-[APPLocalNotification add:]_block_invoke + 207
    8   libdispatch.dylib                   0x0266f7b8 _dispatch_call_block_and_release + 15
    9   libdispatch.dylib                   0x026844d0 _dispatch_client_callout + 14
    10  libdispatch.dylib                   0x02672eb7 _dispatch_root_queue_drain + 291
    11  libdispatch.dylib                   0x02673127 _dispatch_worker_thread2 + 39
    12  libsystem_pthread.dylib             0x029d0dab _pthread_wqthread + 336
    13  libsystem_pthread.dylib             0x029d4cce start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException

UPDATE: I have deployed this app to a real device & get the same error -

[NSNull doubleValue]: unrecognized selector sent to instance o 

on line

double timestamp = [[options objectForKey:@"date"] doubleValue];

Upvotes: 1

Views: 5883

Answers (1)

Adam
Adam

Reputation: 2077

Thanks for the update, Will. Some of the resources I'm finding online speak to NOT using ngCordova, but rather calling the actual plugin. Here are a list of things to try (fingers crossed):

  1. Call a function (from ngCordova's docs, first example):

    .controller('AboutCtrl', function($scope,  $cordovaLocalNotification) {
      $scope.addMessage = function() {
        $cordovaLocalNotification.add({ message: 'Great app!' });
      };
    });
    
  2. Use the plugin, not $cordovaLocalNotification, from this link:

    // Also need to expose window.plugin.not... in config, see link
    
    $scope.addNotification = function(tit, msg) {
      window.plugin.notification.local.add({
          id: 'MYLN',
          title:   tit,
          message: msg,
          icon: 'ic_notification',
          smallIcon: 'ic_notification_small'
      });
    };
    
  3. Change the plugin code from this SO answer:

Replace plugin code screen shot

  1. Try to use a run block from this (non-successful) answer:

    angular.module("starter", ['ionic'])
      .run(function($rootScope, $location, $ionicPlatform, $state) {
          $ionicPlatform.ready(function() {
          $cordovaLocalNotification.add({
              id: '1',
              message: "Push!!"
          })
      }, false);  
    });
    
  2. (All else has failed) Submit an issue with ngCordova. Here is a link for current issues open with $cordovaLocalNotifications.

Upvotes: 2

Related Questions