mirror of
https://github.com/purplecabbage/phonegap-plugins.git
synced 2026-01-25 05:58:17 -05:00
56 lines
2.1 KiB
Objective-C
56 lines
2.1 KiB
Objective-C
//
|
|
// LocalNotification.m
|
|
// Phonegap LocalNotification Plugin
|
|
// Copyright (c) Greg Allen 2011
|
|
// MIT Licensed
|
|
|
|
#import "LocalNotification.h"
|
|
|
|
|
|
@implementation LocalNotification
|
|
- (void)addNotification:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
|
|
double timestamp = [[options objectForKey:@"date"] doubleValue];
|
|
NSString *msg = [options objectForKey:@"message"];
|
|
NSString *action = [options objectForKey:@"action"];
|
|
NSString *notificationId = [options objectForKey:@"id"];
|
|
NSInteger badge = [[options objectForKey:@"badge"] intValue];
|
|
bool hasAction = ([[options objectForKey:@"hasAction"] intValue] == 1)?YES:NO;
|
|
|
|
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
|
|
|
|
UILocalNotification *notif = [[UILocalNotification alloc] init];
|
|
notif.fireDate = date;
|
|
notif.hasAction = hasAction;
|
|
notif.timeZone = [NSTimeZone defaultTimeZone];
|
|
|
|
notif.alertBody = ([msg isEqualToString:@""])?nil:msg;
|
|
notif.alertAction = action;
|
|
notif.soundName = UILocalNotificationDefaultSoundName;
|
|
notif.applicationIconBadgeNumber = badge;
|
|
|
|
NSDictionary *userDict = [NSDictionary dictionaryWithObject:notificationId
|
|
forKey:@"notificationId"];
|
|
notif.userInfo = userDict;
|
|
|
|
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
|
|
NSLog(@"Notification Set: %@ (ID: %@, Badge: %i)", date, notificationId, badge);
|
|
[notif release];
|
|
}
|
|
|
|
- (void)cancelNotification:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
|
|
NSString *notificationId = [arguments objectAtIndex:0];
|
|
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
|
|
for (UILocalNotification *notification in notifications) {
|
|
NSString *notId = [notification.userInfo objectForKey:@"notificationId"];
|
|
if ([notificationId isEqualToString:notId]) {
|
|
NSLog(@"Notification Canceled: %@", notificationId);
|
|
[[UIApplication sharedApplication] cancelLocalNotification:notification];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void)cancelAllNotifications:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
|
|
NSLog(@"All Notifications cancelled");
|
|
[[UIApplication sharedApplication] cancelAllLocalNotifications];
|
|
}
|
|
@end |