Files
Drew Dahlman 2e175f3387 Updates to LocalNotifications
- Added the ability to have the notification ID included in the
callback.

- Fixed spelling of 'foreground'

- updated read me, as well as supporting blogpost
2012-05-16 08:33:03 -06:00

74 lines
3.0 KiB
Objective-C
Executable File

//
// LocalNotification.m
// Phonegap LocalNotification Plugin
// Copyright (c) Greg Allen 2011 & 2012 Drew Dahlman
// MIT Licensed
#import "LocalNotification.h"
@implementation LocalNotification
- (void)addNotification:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
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:@""];
// notif settings
double timestamp = [[options objectForKey:@"date"] doubleValue];
NSString *msg = [options objectForKey:@"message"];
NSString *action = [options objectForKey:@"action"];
NSString *notificationId = [options objectForKey:@"id"];
NSString *sound = [options objectForKey:@"sound"];
NSString *bg = [options objectForKey:@"background"];
NSString *fg = [options objectForKey:@"foreground"];
NSString *repeat = [options objectForKey:@"repeat"];
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.repeatInterval = [[repeatDict objectForKey: repeat] intValue];
notif.alertBody = ([msg isEqualToString:@""])?nil:msg;
notif.alertAction = action;
notif.soundName = sound;
notif.applicationIconBadgeNumber = badge;
NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notificationId,@"notificationId",bg,@"background",fg,@"foreground",nil];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
NSLog(@"Notification Set: %@ (ID: %@, Badge: %i, sound: %@,BACKGROUND: %@, FOREGROUND: %@)", date, notificationId, badge, sound,bg,fg);
//[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