IonicFramework的本地通知

时间:2014-12-23 00:47:26

标签: angularjs ionic-framework

我有一个表格,用户可以勾选哪些日期和时间来设置闹钟。表格保存了它的设置。

我想现在根据设置添加本地通知。我使用了ngCordova插件来做到这一点。该代码不会引发错误,但我无法收到通知。

这就是我的控制器的样子。

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("/");
  };
})

我该如何开始调试?我哪里可能出错?

我已尝试直接在这样的控制器中运行即时通知

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

然而,使用以下堆栈跟踪崩溃模拟器应用程序

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)

更新

我尝试使用window.plugin直接添加通知,如下所示:

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

但是我收到了错误:

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

更新:我已将此应用程序部署到真实设备&amp;得到同样的错误 -

[NSNull doubleValue]: unrecognized selector sent to instance o 

在线

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

1 个答案:

答案 0 :(得分:2)

感谢您的更新,Will。我在网上发现的一些资源不使用ngCordova,而是使用实际的插件。以下是要尝试的事项列表(手指交叉):

  1. 调用一个函数(来自ngCordova&#39; s docs,第一个例子):

    .controller('AboutCtrl', function($scope,  $cordovaLocalNotification) {
      $scope.addMessage = function() {
        $cordovaLocalNotification.add({ message: 'Great app!' });
      };
    });
    
  2. 使用此link中的插件而不是$cordovaLocalNotification

    // 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. 从此SO answer更改插件代码:

  4. Replace plugin code screen shot

    1. 尝试使用此(non-successful) answer中的run块:

      angular.module("starter", ['ionic'])
        .run(function($rootScope, $location, $ionicPlatform, $state) {
            $ionicPlatform.ready(function() {
            $cordovaLocalNotification.add({
                id: '1',
                message: "Push!!"
            })
        }, false);  
      });
      
    2. (其他一切都失败了)使用ngCordova提交问题。以下是使用$cordovaLocalNotifications打开的当前问题的链接。