UILocalNotification的警报行动代码 [英] Code for alert action of UILocalNotification

查看:135
本文介绍了UILocalNotification的警报行动代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [self.datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];

notif.alertBody = @"Did you forget something?";
notif.alertAction = @"Show me";

如果用户点击showme应用程序应该打开,他应该收到警报。
我应该在哪里写这段代码?如果可能的话,有人请给我一些代码

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

推荐答案

你会根据应用程序在触发通知时的状态,在两个地方获取有关 UILocalNotification 的通知。

You will get the notification about the UILocalNotification in two places depending on app's state at the time the notification is fired.

1.在应用程序中:didFinishLaunchingWithOptions:方法,如果应用既不运行也不在后台。

1.In application:didFinishLaunchingWithOptions: method, if the app is neither running nor in the background.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    ...
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];  
    if (localNotif) {       
        // Show Alert Here
    }
    ...
}

2.在应用程序:didReceiveLocalNotification:方法中,如果应用程序正在运行或在后台运行。在应用程序运行时显示警报几乎没用。因此,只有在通知触发时应用程序处于后台时才必须显示警报。要知道应用程序是否从后台恢复,请使用 applicationWillEnterForeground:方法。

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.

- (void)applicationWillEnterForeground:(UIApplication *)application {

    isAppResumingFromBackground = YES;
}

使用此功能,您可以在 didReceiveLocalNotification中显示提醒:仅当应用程序从后台恢复时的方法。

Using this you can show the alert in didReceiveLocalNotification: method only when the app is resuming from background.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

    if (isAppResumingFromBackground) {

        // Show Alert Here
    }
}

如果要在通知被触发时始终显示警报视图,则可以省略 if-condition 应用程序的状态。

You can simply omit the if-condition if you want to show the alert view all the time the notification is fired regardless of the app's state.

这篇关于UILocalNotification的警报行动代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆