问题描述
如何使用各种警报正文重复 UILocalNotification?
How Can I repeat UILocalNotification with various alert body?
例如:
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?
谢谢.
推荐答案
您可以在 AppDelegate 中实现 application:didReceiveLocalNotification
方法,并增加一个day counter"变量.然后,为通知的警报正文安排一个新的 UILocalNotification
和一个字符串数组.使用日计数器获取更新的字符串.下面是一些示例代码:
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:
在你的 AppDelegate.h 中:
In your AppDelegate.h:
@property (assign, nonatomic) int dayCount;
在您的 AppDelegate.m 中:
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.
这篇关于带有各种警报正文的 UILocalNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!