ios UILocalNotification 警报动作的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8008235/
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
Code for alert action of UILocalNotification
提问by Chandu
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [self.datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"Did you forget something?";
notif.alertAction = @"Show me";
if the user clicks on "showme" the app should open and he should get the alert. Where should i write this code?and if possible someone please give me a little bit of code
如果用户点击“showme”,应用程序应该打开并且他应该收到警报。我应该在哪里写这段代码?如果可能的话,有人请给我一点代码
回答by EmptyStack
You will get the notification about the UILocalNotificationin two places depending on app's state at the time the notification is fired.
您将在两个位置收到有关UILocalNotification的通知,具体取决于触发通知时的应用程序状态。
1.In application:didFinishLaunchingWithOptions:method, if the app is neither running nor in the background.
1.在application:didFinishLaunchingWithOptions:方法中,如果应用程序既不在运行也不在后台。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
// Show Alert Here
}
...
}
2.In application:didReceiveLocalNotification:method if the app is either running or in background. Its almost useless to show the alert when the app is already running. So you have to show the alert only when the app was in background at the time the notification fired. To know if the app is resuming from background use the applicationWillEnterForeground:method.
2.在应用程序中:didReceiveLocalNotification:如果应用程序正在运行或在后台运行的方法。当应用程序已经运行时显示警报几乎没有用。因此,您必须仅在通知触发时应用程序处于后台时才显示警报。要知道应用程序是否从后台恢复,请使用applicationWillEnterForeground:方法。
- (void)applicationWillEnterForeground:(UIApplication *)application {
isAppResumingFromBackground = YES;
}
Using this you can show the alert in didReceiveLocalNotification:method only when the app is resuming from background.
使用它,您可以仅在应用程序从后台恢复时在didReceiveLocalNotification:方法中显示警报。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (isAppResumingFromBackground) {
// Show Alert Here
}
}
You can simply omit the if-conditionif you want to show the alert view all the time the notification is fired regardless of the app's state.
如果您想在通知被触发时一直显示警报视图,而不管应用程序的状态如何,您可以简单地省略if 条件。
回答by user755278
Add a function which we will call on the touch of the button inside the YourViewController.h file and then give the body to that function in the YourViewController.m file
在 YourViewController.h 文件中添加一个我们将在触摸按钮时调用的函数,然后在 YourViewController.m 文件中为该函数提供主体
-(void)Trigger_LocalNotification
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *_localNotification = [[UILocalNotification alloc]init];
//setting the fire dat of the local notification
_localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
//setting the time zone
_localNotification.timeZone = [NSTimeZone defaultTimeZone];
//setting the message to display
_localNotification.alertBody = @"Did you forget something?";
//default notification sound
_localNotification.soundName = UILocalNotificationDefaultSoundName;
//displaying the badge number
_localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
//schedule a notification at its specified time with the help of the app delegate
[[UIApplication sharedApplication]scheduleLocalNotification:_localNotification];
}
The first line of the code removes all the local notification from the system if they are declared. In the second line I am initializing the UILocalNotification variable and in the third line I am using the fireDate property to set the time when this local notification will trigger and as you can see that the notification will be triggered after 5 seconds.
代码的第一行从系统中删除所有已声明的本地通知。在第二行中,我正在初始化 UILocalNotification 变量,在第三行中,我使用 fireDate 属性来设置触发此本地通知的时间,您可以看到通知将在 5 秒后触发。
The soundName is a property of the UILocalNotification class which is used to play a sound when the notification is triggered and when the app firing this local notification is not active then in that case a alert box will pop up with the default notification sound and the alert message is written in using the property alertBody. The last line of the code will attach this notification with the system.
soundName 是 UILocalNotification 类的一个属性,用于在触发通知时播放声音,并且当触发此本地通知的应用程序未处于活动状态时,在这种情况下,将弹出一个带有默认通知声音和警报的警报框消息是使用属性 alertBody 写入的。代码的最后一行会将此通知附加到系统中。
make sure to attach this function with the button touch up inside event
确保在事件中使用按钮触摸附加此功能
[btn addTarget:self action:@selector(Trigger_LocalNotification) forControlEvents:UIControlEventTouchUpInside];
Now select the App Delegate.m file of your project and create the object of this class (YourViewController)
现在选择你项目的 App Delegate.m 文件并创建这个类的对象(YourViewController)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
YourViewController *obj = [[YourViewController alloc]init];
[self.window addSubview:obj.view];
[self.window makeKeyAndVisible];
return YES;
}
Run the application and when the app is launched in the simulator then quickly press the home button to see the alert box of the local notification after 5 sec.
运行应用程序,当应用程序在模拟器中启动时,然后快速按下主页按钮,在 5 秒后看到本地通知的警告框。
I hope that this answer has helped you out in learning how to implement UILocalNotification.
我希望这个答案能帮助你学习如何实现 UILocalNotification。
回答by Tendulkar
there is a delegate method called didreceivelocalnotification.You have to write this in the app delegate.And when the user clicks then this delegate method will be going to call.So write any code in the didreceivelocalnotifaction method.
有一个称为 didreceivelocalnotification 的委托方法。您必须在应用程序委托中编写此方法。当用户单击时,将调用此委托方法。因此,请在 didreceivelocalnotifaction 方法中编写任何代码。
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
}