喂,
我正在寻找一种制作“本地”推送通知的方法。我无法弄清楚我应该怎么做,所以我正在寻求一些帮助。我需要的是:
我真的希望你们中的一个有时间来帮助我,谢谢!
答案 0 :(得分:44)
这很简单:
1)当应用关闭时,安排将在24小时内启动的本地通知
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24];
notification.alertBody = @"24 hours passed since last visit :(";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
2)如果应用程序已打开(在本地通知触发之前),请取消本地通知
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
答案 1 :(得分:2)
您可以将UILocalNotification用于此目的。
并实施您的UIApplication applicationWillTerminate
和applicationDidEnterBackground
代表:
- (void)applicationWillTerminate:(UIApplication *)application
{
[self scheduleNotification];
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self scheduleNotification];
}
- (void)scheduleNotification
{
UILocalNotification *locNot = [[UILocalNotification alloc] init];
locNot.fireDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24];
[[UIApplication sharedApplication] scheduleLocalNotification: locNot];
}
当您进入应用程序时,您需要取消此通知。所以实现applicationDidBecomeActive
之类的;
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
答案 2 :(得分:1)
UILocalNotification
符合您的要求Apple doc