当应用程序处于前台并且当前正在iPhone SDK中运行时,是否会显示本地通知?
答案 0 :(得分:9)
不,您将在appdelegate收到通知。
- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {
//Place your code to handle the notification here.
}
答案 1 :(得分:3)
我创建了一个lib来制作与本地通知几乎相同的动画。
检查一下: https://github.com/OpenFibers/OTNotification
演示:
当您在
中收到消息时,可以向此lib发布新消息- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
OTNotificationManager *notificationManager = [OTNotificationManager defaultManager];
OTNotificationMessage *notificationMessage = [[OTNotificationMessage alloc] init];
notificationMessage.title = [self notificationTitle];
notificationMessage.message = @"A notification. Touch me to hide me.";
[notificationManager postNotificationMessage:notificationMessage];
}
答案 2 :(得分:2)
接受的anser是对的,但是接收所有通知并向用户显示内容是不够的
- (void) application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification {
您必须检查,是否是当前通知。 有时会触发另一个通知(例如,当您取消它们时)。所以,你必须检查,这是你除了:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
if (fabs([[NSDate date] timeIntervalSinceDate:[notification fireDate]]) <= 0.5f)
{
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Notification alert", @"")
message:notification.alertBody
delegate:self
cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
}
}
答案 3 :(得分:0)
如果您的应用当前处于前台,则会在您的代理中调用以下函数:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)Notifikation
然后您可以决定显示警报视图,但标准视图不会自动显示
答案 4 :(得分:0)
Swift 2.2:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
var state = application.applicationState
if state == .Active {
// handle the notification, e.g. show an alert
}
}
Swift 3.0:
func application(_ application: UIApplication, didReceive notification: UILocalNotification) {
var state: UIApplicationState = application.applicationState
if state == .active {
// handle the notification, e.g. show an alert
}
}