显示本地通知时没有didFinishLaunchingWithOptions:

时间:2012-06-10 16:15:34

标签: cocoa

我对显示本地通知有很明显的理解。 就我在其他线程中阅读而言,首先要创建并安排应用程序的本地通知。

要显示该通知,必须使用委托didFinishLaunchingWithOptions :(如果app处于后台操作中)和didReceiveLocalNotification :(如果app位于前台)。

现在即使我没有更改didFinishLaunchingWithOptions:方法,当我的应用程序在后台时,通知已经被查看。

如果didFinishLaunchingWithOptions:在我完全指定它时至少会使用它,那不会是一个大问题。但事实并非如此。

所以我的问题是,即使我没有使用didFinishLaunchingWithOptions:方法,也会显示通知。当用户点击通知时,应用程序将进入前台并触发didReceiveLocalNotification:方法,并再次显示通知。

我最初想要做的是在执行didFinishLaunchingWithOptions:时取消AllLocalNotifications,但由于它没有被执行,我有点被困在这里。

好的,applicationWillEnterForeground可能有一个解决方法:但老实说,我想了解,为什么即使没有在didFinishLaunchingWithOptions中指定通知也会显示通知:。

你所有的帮助都是真正的帮助!!谢谢!

//
//  myNotificationsClass.m
//


#import "myNotificationsClass.h"

@implementation myNotificationsClass

//Sets up a Local Notification with Message, TimeFromNow, BadgeNumber and UserInfo

//no Class Instance for calling this method needed!!

+ (void)setupLocalNotificationsWithMessage: (NSString *) message andTimeFromNow: (NSTimeInterval) seconds andAlertAction: (NSString *) alertAction andBadgeNumber: (NSInteger) badgeNumber andUserInfo: (NSDictionary *) infoDict {

//[[UIApplication sharedApplication] cancelAllLocalNotifications];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

// create date/time information
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
localNotification.timeZone = [NSTimeZone defaultTimeZone];

//setup Appearence and Message
localNotification.alertBody = message; //@"Time to get up!";
localNotification.alertAction = alertAction;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = badgeNumber;

localNotification.userInfo = infoDict;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

@end


//overwrites the viewWillAppear: Method from the primary Class to display a Test Notification

@implementation UIViewController (localNotification)
- (void)viewWillAppear:(BOOL)animated {

    [myNotificationsClass setupLocalNotificationsWithMessage:@"First Test after 2 Seconds" andTimeFromNow:2 andAlertAction:@"GoTo iSnah" andBadgeNumber:7 andUserInfo:nil];

}
@end

//receive Local Notifications even if the App is in Foreground
//overwrites the primery method
@implementation UIResponder (localNotificationForeground)

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

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[[[NSBundle mainBundle] infoDictionary]   objectForKey:@"CFBundleName"]
                                                        message:notification.alertBody
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];

        [alertView show];

    //reset Badge
    application.applicationIconBadgeNumber = 0;

}

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


    //Because I don't want the Notification to be displayed twice 
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if (notification) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[[[NSBundle mainBundle] infoDictionary]   objectForKey:@"CFBundleName"]
                                                            message:notification.alertBody
                                                            delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles:nil];


        [alertView show];

        //reset Badge
        application.applicationIconBadgeNumber = 0;

    }


    return YES;
}

@end

1 个答案:

答案 0 :(得分:1)

你非常接近。如果应用程序在后台运行,则响应通知的用户将不会导致应用程序“启动”。 “启动”表示应用程序根本不运行(前景或后台)。因此,当用户响应您的本地通知时,只有在您启动或启动应用程序时才会执行代码。

因此,您需要为didReceiveLocalNotification方法检查应用程序状态,以便在用户响应本地通知时查看它是在前台还是在后台。

您可能会执行以下操作来区分前景和背景之间的逻辑:

- (void)application:(UIApplication *)application didReceiveLocalNotification:    (UILocalNotification *)notification
if ( application.applicationState == UIApplicationStateActive ){
    NSLog(@"Was already in the foreground");
}
else{
    NSLog(@"Was in the background");
}

}

然而,

小点。文档说明在应用程序之后调用didReceiveLocalNotification方法:didFinishLaunchingWithOptions :(如果实现了该方法)。因此,如果您实施了这两种方法,则需要确保2的组合在“启动”情况下正常工作。