0

我对显示本地通知有一个很好的理解问题。就我在其他线程中阅读的内容而言,首先要使用应用程序创建和安排本地通知。

为了显示该通知,必须使用代表 didFinishLaunchingWithOptions:(如果应用程序处于后台操作)和 didReceiveLocalNotification:(如果应用程序处于前台)。

现在即使我没有更改 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
4

1 回答 1

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");
}

}

然而,次要的一点。文档指出在 application:didFinishLaunchingWithOptions: 之后调用了 didReceiveLocalNotification 方法:(如果实现了该方法)。因此,如果您实现了这两种方法,则需要确保这两种方法的组合在“启动”情况下正常工作。

于 2012-08-14T22:28:13.773 回答