14

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

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

我真的希望你们有时间帮助我,谢谢!

4

3 回答 3

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];
}
于 2013-06-27T09:24:27.977 回答
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];
}
于 2013-06-27T09:21:35.030 回答
1

UILocalNotification将满足您的要求Apple doc

于 2013-06-27T09:09:42.580 回答