-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLocalNotification.m
86 lines (56 loc) · 3.11 KB
/
LocalNotification.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*!
* Cordova 2.3.0+ LocalNotification plugin
* Original author: Olivier Lesnicki
*/
#import "LocalNotification.h"
#import <Cordova/CDV.h>
@implementation LocalNotification
-(void)addNotification:(CDVInvokedUrlCommand*)command {
NSMutableDictionary *repeatDict = [[NSMutableDictionary alloc] init];
[repeatDict setObject:[NSNumber numberWithInt:NSDayCalendarUnit ] forKey:@"daily" ];
[repeatDict setObject:[NSNumber numberWithInt:NSWeekCalendarUnit ] forKey:@"weekly" ];
[repeatDict setObject:[NSNumber numberWithInt:NSMonthCalendarUnit ] forKey:@"monthly" ];
[repeatDict setObject:[NSNumber numberWithInt:NSYearCalendarUnit ] forKey:@"yearly" ];
[repeatDict setObject:[NSNumber numberWithInt:0] forKey:@"" ];
UILocalNotification* notif = [[UILocalNotification alloc] init];
double fireDate = [[command.arguments objectAtIndex:0] doubleValue];
NSString *alertBody = [command.arguments objectAtIndex:1];
NSNumber *repeatInterval = [command.arguments objectAtIndex:2];
NSString *soundName = [command.arguments objectAtIndex:3];
NSString *notificationId = [command.arguments objectAtIndex:4];
notif.alertBody = ([alertBody isEqualToString:@""])?nil:alertBody;
notif.fireDate = [NSDate dateWithTimeIntervalSince1970:fireDate];
notif.repeatInterval = [[repeatDict objectForKey: repeatInterval] intValue];
notif.soundName = soundName;
notif.timeZone = [NSTimeZone defaultTimeZone];
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:
notificationId , @"notificationId",
command.callbackId, @"callbackId",
nil
];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
- (void)cancelNotification:(CDVInvokedUrlCommand*)command {
NSString *notificationId = [command.arguments objectAtIndex:0];
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *notification in notifications) {
NSString *notId = [notification.userInfo objectForKey:@"notificationId"];
if ([notificationId isEqualToString:notId]) {
[[UIApplication sharedApplication] cancelLocalNotification: notification];
}
}
}
- (void)cancelAllNotifications:(CDVInvokedUrlCommand*)command {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
- (void)didReceiveLocalNotification:(NSNotification *)notification
{
UILocalNotification* notif = [notification object];
CDVPluginResult* pluginResult = nil;
NSString* javaScript = nil;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: [notif.userInfo objectForKey:@"notificationId"]];
javaScript = [pluginResult toSuccessCallbackString: [notif.userInfo objectForKey:@"callbackId"]];
[self writeJavascript:javaScript];
}
@end