APPLocalNotification.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Copyright (c) 2013 by appPlant GmbH. All rights reserved.
  3. *
  4. * @APPPLANT_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPPLANT_LICENSE_HEADER_END@
  22. */
  23. #import "APPLocalNotification.h"
  24. #import "APPLocalNotificationOptions.h"
  25. #import "UNUserNotificationCenter+APPLocalNotification.h"
  26. #import "UNNotificationRequest+APPLocalNotification.h"
  27. #import "UNMutableNotificationContent+APPLocalNotification.h"
  28. @interface APPLocalNotification ()
  29. @property (strong, nonatomic) UIApplication* app;
  30. @property (strong, nonatomic) UNUserNotificationCenter* center;
  31. @property (readwrite, assign) BOOL deviceready;
  32. @property (readonly, nonatomic, retain) NSMutableArray* eventQueue;
  33. @end
  34. @implementation APPLocalNotification
  35. @synthesize deviceready, eventQueue;
  36. #pragma mark -
  37. #pragma mark Interface
  38. /**
  39. * Execute all queued events.
  40. *
  41. * @return [ Void ]
  42. */
  43. - (void) deviceready:(CDVInvokedUrlCommand*)command
  44. {
  45. deviceready = YES;
  46. for (NSString* js in eventQueue) {
  47. [self.commandDelegate evalJs:js];
  48. }
  49. [eventQueue removeAllObjects];
  50. }
  51. /**
  52. * Schedule notifications.
  53. *
  54. * @param [Array<Hash>] properties A list of key-value properties.
  55. *
  56. * @return [ Void ]
  57. */
  58. - (void) schedule:(CDVInvokedUrlCommand*)command
  59. {
  60. NSArray* notifications = command.arguments;
  61. [self.commandDelegate runInBackground:^{
  62. for (NSDictionary* options in notifications) {
  63. UNMutableNotificationContent* notification;
  64. notification = [[UNMutableNotificationContent alloc]
  65. initWithOptions:options];
  66. [self scheduleNotification:notification];
  67. }
  68. [self execCallback:command];
  69. }];
  70. }
  71. ///**
  72. // * Update a set of notifications.
  73. // *
  74. // * @param properties
  75. // * A dict of properties for each notification
  76. // */
  77. //- (void) update:(CDVInvokedUrlCommand*)command
  78. //{
  79. // NSArray* notifications = command.arguments;
  80. //
  81. // [self.commandDelegate runInBackground:^{
  82. // if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
  83. // for (NSDictionary* options in notifications) {
  84. // NSNumber* id = [options objectForKey:@"id"];
  85. // UNNotificationRequest* notification;
  86. //
  87. // notification = [self.center getNotificationWithId:id];
  88. //
  89. // if (!notification)
  90. // continue;
  91. //
  92. // // [self updateNotification:[notification copy]
  93. // // withOptions:options];
  94. // //
  95. // // [self fireEvent:@"update" notification:notification];
  96. // //
  97. // // if (notifications.count > 1) {
  98. // // [NSThread sleepForTimeInterval:0.01];
  99. // // }
  100. // }
  101. // } else {
  102. // for (NSDictionary* options in notifications) {
  103. // NSNumber* id = [options objectForKey:@"id"];
  104. // UILocalNotification* notification;
  105. //
  106. // notification = [self.app localNotificationWithId:id];
  107. //
  108. // if (!notification)
  109. // continue;
  110. //
  111. // [self updateLocalNotification:[notification copy]
  112. // withOptions:options];
  113. //
  114. // [self fireEvent:@"update" localnotification:notification];
  115. //
  116. // if (notifications.count > 1) {
  117. // [NSThread sleepForTimeInterval:0.01];
  118. // }
  119. // }
  120. // }
  121. //
  122. // [self execCallback:command];
  123. // }];
  124. //}
  125. //
  126. ///**
  127. // * Cancel a set of notifications.
  128. // *
  129. // * @param ids
  130. // * The IDs of the notifications
  131. // */
  132. //- (void) cancel:(CDVInvokedUrlCommand*)command
  133. //{
  134. // [self.commandDelegate runInBackground:^{
  135. // if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
  136. // for (NSNumber* id in command.arguments) {
  137. // UNNotificationRequest* notification;
  138. //
  139. // notification = [self.center getNotificationWithId:id];
  140. //
  141. // if (!notification)
  142. // continue;
  143. //
  144. // [self.center cancelNotification:notification];
  145. // [self fireEvent:@"cancel" notification:notification];
  146. // }
  147. // } else {
  148. // for (NSNumber* id in command.arguments) {
  149. // UILocalNotification* notification;
  150. //
  151. // notification = [self.app localNotificationWithId:id];
  152. //
  153. // if (!notification)
  154. // continue;
  155. //
  156. // [self.app cancelLocalNotification:notification];
  157. // [self fireEvent:@"cancel" localnotification:notification];
  158. // }
  159. // }
  160. //
  161. // [self execCallback:command];
  162. // }];
  163. //}
  164. //
  165. ///**
  166. // * Cancel all local notifications.
  167. // */
  168. //- (void) cancelAll:(CDVInvokedUrlCommand*)command
  169. //{
  170. // [self.commandDelegate runInBackground:^{
  171. // [self cancelAllNotifications];
  172. // [self fireEvent:@"cancelall"];
  173. // [self execCallback:command];
  174. // }];
  175. //}
  176. //
  177. ///**
  178. // * Clear a set of notifications.
  179. // *
  180. // * @param ids
  181. // * The IDs of the notifications
  182. // */
  183. //- (void) clear:(CDVInvokedUrlCommand*)command
  184. //{
  185. // [self.commandDelegate runInBackground:^{
  186. // if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
  187. // for (NSNumber* id in command.arguments) {
  188. // UNNotificationRequest* notification;
  189. //
  190. // notification = [self.center getNotificationWithId:id];
  191. //
  192. // if (!notification)
  193. // continue;
  194. //
  195. // [self.center clearNotification:notification];
  196. // [self fireEvent:@"clear" notification:notification];
  197. // }
  198. // } else {
  199. // for (NSNumber* id in command.arguments) {
  200. // UILocalNotification* notification;
  201. //
  202. // notification = [self.app localNotificationWithId:id];
  203. //
  204. // if (!notification)
  205. // continue;
  206. //
  207. // [self.app clearLocalNotification:notification];
  208. // [self fireEvent:@"clear" localnotification:notification];
  209. // }
  210. // }
  211. //
  212. // [self execCallback:command];
  213. // }];
  214. //}
  215. //
  216. ///**
  217. // * Clear all local notifications.
  218. // */
  219. //- (void) clearAll:(CDVInvokedUrlCommand*)command
  220. //{
  221. // [self.commandDelegate runInBackground:^{
  222. // [self clearAllNotifications];
  223. // [self fireEvent:@"clearall"];
  224. // [self execCallback:command];
  225. // }];
  226. //}
  227. //
  228. ///**
  229. // * If a notification by ID is present.
  230. // *
  231. // * @param id
  232. // * The ID of the notification
  233. // */
  234. //- (void) isPresent:(CDVInvokedUrlCommand *)command
  235. //{
  236. // [self isPresent:command type:NotifcationTypeAll];
  237. //}
  238. //
  239. ///**
  240. // * If a notification by ID is scheduled.
  241. // *
  242. // * @param id
  243. // * The ID of the notification
  244. // */
  245. //- (void) isScheduled:(CDVInvokedUrlCommand*)command
  246. //{
  247. // [self isPresent:command type:NotifcationTypeScheduled];
  248. //}
  249. //
  250. ///**
  251. // * Check if a notification with an ID is triggered.
  252. // *
  253. // * @param id
  254. // * The ID of the notification
  255. // */
  256. //- (void) isTriggered:(CDVInvokedUrlCommand*)command
  257. //{
  258. // [self isPresent:command type:NotifcationTypeTriggered];
  259. //}
  260. //
  261. ///**
  262. // * Check if a notification with an ID exists.
  263. // *
  264. // * @param type
  265. // * The notification life cycle type
  266. // */
  267. //- (void) isPresent:(CDVInvokedUrlCommand*)command
  268. // type:(APPNotificationType)type;
  269. //{
  270. // [self.commandDelegate runInBackground:^{
  271. // NSNumber* id = [command argumentAtIndex:0];
  272. // BOOL exist;
  273. //
  274. // if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
  275. // exist = [self.center notificationExist:id type:type];
  276. // } else {
  277. // if (type == NotifcationTypeAll) {
  278. // exist = [self.app localNotificationExist:id];
  279. // } else {
  280. // exist = [self.app localNotificationExist:id type:type];
  281. // }
  282. // }
  283. //
  284. // CDVPluginResult* result;
  285. // result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  286. // messageAsBool:exist];
  287. //
  288. // [self.commandDelegate sendPluginResult:result
  289. // callbackId:command.callbackId];
  290. // }];
  291. //}
  292. //
  293. ///**
  294. // * List all ids from all local notifications.
  295. // */
  296. //- (void) getAllIds:(CDVInvokedUrlCommand*)command
  297. //{
  298. // [self getIds:command byType:NotifcationTypeAll];
  299. //}
  300. //
  301. ///**
  302. // * List all ids from all pending notifications.
  303. // */
  304. //- (void) getScheduledIds:(CDVInvokedUrlCommand*)command
  305. //{
  306. // [self getIds:command byType:NotifcationTypeScheduled];
  307. //}
  308. //
  309. ///**
  310. // * List all ids from all triggered notifications.
  311. // */
  312. //- (void) getTriggeredIds:(CDVInvokedUrlCommand*)command
  313. //{
  314. // [self getIds:command byType:NotifcationTypeTriggered];
  315. //}
  316. //
  317. ///**
  318. // * List of ids for given local notifications.
  319. // *
  320. // * @param type
  321. // * Notification life cycle type
  322. // * @param ids
  323. // * The IDs of the notifications
  324. // */
  325. //- (void) getIds:(CDVInvokedUrlCommand*)command
  326. // byType:(APPNotificationType)type;
  327. //{
  328. // [self.commandDelegate runInBackground:^{
  329. // NSArray* ids;
  330. //
  331. // if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
  332. // ids = [self.center getNotificationIdsByType:type];
  333. // } else {
  334. // if (type == NotifcationTypeAll) {
  335. // ids = [self.app localNotificationIds];
  336. // } else {
  337. // ids = [self.app localNotificationIdsByType:type];
  338. // }
  339. // }
  340. //
  341. // CDVPluginResult* result;
  342. // result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  343. // messageAsArray:ids];
  344. //
  345. // [self.commandDelegate sendPluginResult:result
  346. // callbackId:command.callbackId];
  347. // }];
  348. //}
  349. //
  350. ///**
  351. // * Propertys for given local notification.
  352. // */
  353. //- (void) getSingle:(CDVInvokedUrlCommand*)command
  354. //{
  355. // [self getOption:command byType:NotifcationTypeAll];
  356. //}
  357. //
  358. ///**
  359. // * Propertya for given scheduled notification.
  360. // */
  361. //- (void) getSingleScheduled:(CDVInvokedUrlCommand*)command
  362. //{
  363. // [self getOption:command byType:NotifcationTypeScheduled];
  364. //}
  365. //
  366. //// Propertys for given triggered notification
  367. //- (void) getSingleTriggered:(CDVInvokedUrlCommand*)command
  368. //{
  369. // [self getOption:command byType:NotifcationTypeTriggered];
  370. //}
  371. //
  372. ///**
  373. // * Property list for given local notifications.
  374. // *
  375. // * @param ids
  376. // * The IDs of the notifications
  377. // */
  378. //- (void) getAll:(CDVInvokedUrlCommand*)command
  379. //{
  380. // [self getOptions:command byType:NotifcationTypeAll];
  381. //}
  382. //
  383. ///**
  384. // * Property list for given scheduled notifications.
  385. // *
  386. // * @param ids
  387. // * The IDs of the notifications
  388. // */
  389. //- (void) getScheduled:(CDVInvokedUrlCommand*)command
  390. //{
  391. // [self getOptions:command byType:NotifcationTypeScheduled];
  392. //}
  393. //
  394. ///**
  395. // * Property list for given triggered notifications.
  396. // *
  397. // * @param ids
  398. // * The IDs of the notifications
  399. // */
  400. //- (void) getTriggered:(CDVInvokedUrlCommand *)command
  401. //{
  402. // [self getOptions:command byType:NotifcationTypeTriggered];
  403. //}
  404. //
  405. ///**
  406. // * Propertys for given triggered notification.
  407. // *
  408. // * @param type
  409. // * Notification life cycle type
  410. // * @param ids
  411. // * The ID of the notification
  412. // */
  413. //- (void) getOption:(CDVInvokedUrlCommand*)command
  414. // byType:(APPNotificationType)type;
  415. //{
  416. // [self.commandDelegate runInBackground:^{
  417. // NSArray* ids = command.arguments;
  418. // NSArray* notifications;
  419. //
  420. // if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
  421. // notifications = [self.center getNotificationOptionsByType:type
  422. // andId:ids];
  423. // } else {
  424. // if (type == NotifcationTypeAll) {
  425. // notifications = [self.app localNotificationOptionsById:ids];
  426. // }
  427. // else {
  428. // notifications = [self.app localNotificationOptionsByType:type
  429. // andId:ids];
  430. // }
  431. // }
  432. //
  433. // CDVPluginResult* result;
  434. // result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  435. // messageAsDictionary:[notifications firstObject]];
  436. //
  437. // [self.commandDelegate sendPluginResult:result
  438. // callbackId:command.callbackId];
  439. // }];
  440. //}
  441. //
  442. ///**
  443. // * Property list for given triggered notifications.
  444. // *
  445. // * @param type
  446. // * Notification life cycle type
  447. // * @param ids
  448. // * The IDs of the notifications
  449. // */
  450. //- (void) getOptions:(CDVInvokedUrlCommand*)command
  451. // byType:(APPNotificationType)type;
  452. //{
  453. // [self.commandDelegate runInBackground:^{
  454. // NSArray* ids = command.arguments;
  455. // NSArray* notifications;
  456. //
  457. // if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
  458. // if (type == NotifcationTypeAll && ids.count == 0) {
  459. // notifications = [self.center getNotificationOptions];
  460. // }
  461. // else if (type == NotifcationTypeAll) {
  462. // notifications = [self.center getNotificationOptionsById:ids];
  463. // }
  464. // else if (ids.count == 0) {
  465. // notifications = [self.center getNotificationOptionsByType:type];
  466. // }
  467. // else {
  468. // notifications = [self.center getNotificationOptionsByType:type
  469. // andId:ids];
  470. // }
  471. // } else {
  472. // if (type == NotifcationTypeAll && ids.count == 0) {
  473. // notifications = [self.app localNotificationOptions];
  474. // }
  475. // else if (type == NotifcationTypeAll) {
  476. // notifications = [self.app localNotificationOptionsById:ids];
  477. // }
  478. // else if (ids.count == 0) {
  479. // notifications = [self.app localNotificationOptionsByType:type];
  480. // }
  481. // else {
  482. // notifications = [self.app localNotificationOptionsByType:type
  483. // andId:ids];
  484. // }
  485. // }
  486. //
  487. // CDVPluginResult* result;
  488. // result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  489. // messageAsArray:notifications];
  490. //
  491. // [self.commandDelegate sendPluginResult:result
  492. // callbackId:command.callbackId];
  493. // }];
  494. //}
  495. /**
  496. * Check for permission to show notifications.
  497. */
  498. - (void) check:(CDVInvokedUrlCommand*)command
  499. {
  500. [_center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings* settings) {
  501. BOOL authorized = settings.authorizationStatus == UNAuthorizationStatusAuthorized;
  502. BOOL enabled = settings.notificationCenterSetting == UNNotificationSettingEnabled;
  503. BOOL permitted = authorized && enabled;
  504. CDVPluginResult* result;
  505. result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
  506. messageAsBool:permitted];
  507. [self.commandDelegate sendPluginResult:result
  508. callbackId:command.callbackId];
  509. }];
  510. }
  511. /**
  512. * Request for permission to show notifcations.
  513. */
  514. - (void) request:(CDVInvokedUrlCommand*)command
  515. {
  516. UNAuthorizationOptions options =
  517. (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert);
  518. [self.center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError* e) {
  519. [self check:command];
  520. }];
  521. }
  522. #pragma mark -
  523. #pragma mark Private
  524. /**
  525. * Schedule the local notification.
  526. */
  527. - (void) scheduleNotification:(UNMutableNotificationContent*)notification
  528. {
  529. __weak APPLocalNotification* weakSelf = self;
  530. UNNotificationRequest* request = notification.request;
  531. [_center addNotificationRequest:request withCompletionHandler:^(NSError* e) {
  532. __strong APPLocalNotification* strongSelf = weakSelf;
  533. [strongSelf fireEvent:@"add" notification:request];
  534. }];
  535. }
  536. ///**
  537. // * Update the local notification.
  538. // */
  539. //- (void) updateNotification:(UILocalNotification*)notification
  540. // withOptions:(NSDictionary*)newOptions
  541. //{
  542. // NSMutableDictionary* options = [notification.userInfo mutableCopy];
  543. //
  544. // [options addEntriesFromDictionary:newOptions];
  545. // [options setObject:[NSDate date] forKey:@"updatedAt"];
  546. //
  547. //// notification = [[UILocalNotification alloc]
  548. //// initWithOptions:options];
  549. ////
  550. //// [self scheduleLocalNotification:notification];
  551. //}
  552. //
  553. ///**
  554. // * Cancel all local notifications.
  555. // */
  556. //- (void) cancelAllNotifications
  557. //{
  558. // if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
  559. // [self.center cancelAllNotifications];
  560. // } else {
  561. // [self.app cancelAllLocalNotifications];
  562. // }
  563. //
  564. // [self.app setApplicationIconBadgeNumber:0];
  565. //}
  566. //
  567. ///**
  568. // * Clear all local notifications.
  569. // */
  570. //- (void) clearAllNotifications
  571. //{
  572. // if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
  573. // [self.center clearAllNotifications];
  574. // } else {
  575. // [self.app clearAllLocalNotifications];
  576. // }
  577. //
  578. // [self.app setApplicationIconBadgeNumber:0];
  579. //}
  580. #pragma mark -
  581. #pragma mark UNUserNotificationCenterDelegate
  582. /**
  583. * Called when a notification is delivered to the app while being in foreground.
  584. */
  585. - (void) userNotificationCenter:(UNUserNotificationCenter *)center
  586. willPresentNotification:(UNNotification *)notification
  587. withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
  588. {
  589. [self fireEvent:@"trigger" notification:notification.request];
  590. completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert);
  591. }
  592. /**
  593. * Called to let your app know which action was selected by the user for a given
  594. * notification.
  595. */
  596. - (void) userNotificationCenter:(UNUserNotificationCenter *)center
  597. didReceiveNotificationResponse:(UNNotificationResponse *)response
  598. withCompletionHandler:(void (^)())completionHandler
  599. {
  600. UNNotificationRequest* notification = response.notification.request;
  601. [self fireEvent:@"click" notification:notification];
  602. // if ([notification.options isRepeating]) {
  603. // [_center clearNotification:notification];
  604. // [self fireEvent:@"clear" notification:notification];
  605. // } else {
  606. // [_center cancelNotification:notification];
  607. // [self fireEvent:@"cancel" notification:notification];
  608. // }
  609. completionHandler();
  610. }
  611. #pragma mark -
  612. #pragma mark Life Cycle
  613. /**
  614. * Registers obervers after plugin was initialized.
  615. */
  616. - (void) pluginInitialize
  617. {
  618. eventQueue = [[NSMutableArray alloc] init];
  619. _app = [UIApplication sharedApplication];
  620. _center = [UNUserNotificationCenter currentNotificationCenter];
  621. _center.delegate = self;
  622. }
  623. #pragma mark -
  624. #pragma mark Helper
  625. /**
  626. * Retrieve the state of the application.
  627. *
  628. * @return "background" or "foreground"
  629. */
  630. - (NSString*) applicationState
  631. {
  632. UIApplicationState state = [_app applicationState];
  633. bool isActive = state == UIApplicationStateActive;
  634. return isActive ? @"foreground" : @"background";
  635. }
  636. /**
  637. * Simply invokes the callback without any parameter.
  638. */
  639. - (void) execCallback:(CDVInvokedUrlCommand*)command
  640. {
  641. CDVPluginResult *result = [CDVPluginResult
  642. resultWithStatus:CDVCommandStatus_OK];
  643. [self.commandDelegate sendPluginResult:result
  644. callbackId:command.callbackId];
  645. }
  646. /**
  647. * Fire general event.
  648. */
  649. - (void) fireEvent:(NSString*)event
  650. {
  651. [self fireEvent:event notification:NULL];
  652. }
  653. /**
  654. * Fire event for local notification.
  655. */
  656. - (void) fireEvent:(NSString*)event
  657. notification:(UNNotificationRequest*)notification
  658. {
  659. NSString* js;
  660. NSString* appState = [self applicationState];
  661. NSString* params = [NSString stringWithFormat:@"\"%@\"", appState];
  662. if (notification) {
  663. NSString* args = [notification encodeToJSON];
  664. params = [NSString stringWithFormat:@"%@,'%@'", args, appState];
  665. }
  666. js = [NSString stringWithFormat:
  667. @"cordova.plugins.notification.local.core.fireEvent('%@', %@)",
  668. event, params];
  669. if (deviceready) {
  670. [self.commandDelegate evalJs:js];
  671. } else {
  672. [self.eventQueue addObject:js];
  673. }
  674. }
  675. @end