iOS“本地”推送通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17339052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS "Local" Push Notification
提问by Mathias M?nsted
Hey,
嘿,
I'm looking for a way to make "local" push notifications. I can't figure out how I should do this, so I'm looking for some help. What I need is:
我正在寻找一种制作“本地”推送通知的方法。我不知道该怎么做,所以我正在寻求帮助。我需要的是:
- a way to send a notification for a user who haven't opened the application within 24 hours. (Or that an inthavent change)
- 一种向 24 小时内未打开应用程序的用户发送通知的方法。(或者一个int没有改变)
I really hope that one of you got time to help me, thanks!
我真的希望你们中的一个人有时间帮助我,谢谢!
回答by tilo
This is pretty straight forward:
这是非常直接的:
1) When the app is closed, schedule a local notification that will fire in 24 hours
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) if the app is opened (before the local notification fires), cancel the local notification
2)如果应用程序打开(在本地通知触发之前),取消本地通知
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
回答by Midhun MP
You can use the UILocalNotificationfor this purpose.
为此,您可以使用UILocalNotification。
And implement your UIApplicationapplicationWillTerminate
and applicationDidEnterBackground
delegates like:
并实现您的UIApplicationapplicationWillTerminate
和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];
}
When you enter to your app you need to cancel this notification. So implement applicationDidBecomeActive
like;
当您进入您的应用程序时,您需要取消此通知。所以实现applicationDidBecomeActive
像;
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}