问题
How Can I repeat UILocalNotification with various alert body?
For example:
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.alertBody = @"Hello";
notif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
By using this code the notification will be repeated daily, how can I repeat the notification daily with different alert body each day?
Thanks.
回答1:
You could implement the application:didReceiveLocalNotification
method in the AppDelegate, and increase a 'day counter' variable. Then, schedule a new UILocalNotification
with an array of strings for your notification's alert body. Use the day counter to get an updated string. Here's some example code:
In your AppDelegate.h:
@property (assign, nonatomic) int dayCount;
In your AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self scheduleLocalNotification];
return YES;
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
self.dayCount++;
[self scheduleLocalNotification];
}
-(void)scheduleLocalNotification{
NSArray *notifTextArray = [NSArray arrayWithObjects:@"Hello", @"Welcome", @"Hi there", nil];
UILocalNotification *notif = [[UILocalNotification alloc] init];
if(self.dayCount < notifTextArray.count){
notif.alertBody = [notifTextArray objectAtIndex:self.dayCount];
}
else{
self.dayCount = 0;
notif.alertBody = [notifTextArray objectAtIndex:self.dayCount];
}
notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:86400]; //86400 seconds in a day
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
Just an option, but hope it helps.
回答2:
Once you have scheduled a local notification, you cannot change any properties of the notification and the alert body as-well.
You may have to cancel the older notification and reschedule a new one to achieve this.
来源:https://stackoverflow.com/questions/18792997/uilocalnotification-with-various-alert-body