iOS“本地”推送通知

时间:2013-06-27 09:07:49

标签: ios objective-c push-notification

我正在寻找一种制作“本地”推送通知的方法。我无法弄清楚我应该怎么做,所以我正在寻求一些帮助。我需要的是:

  • 一种为24小时内未打开应用程序的用户发送通知的方法。 (或者 int 没有改变)

我真的希望你们中的一个有时间来帮助我,谢谢!

3 个答案:

答案 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 applicationWillTerminateapplicationDidEnterBackground代表:

- (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