如何在iPhone中调用didReceiveLocalNotification:(UILocalNotification



我需要后台进程(用于调用webservice)调用didReceiveLocalNotification:(UILocalNotification *)通知一旦应用程序启动状态,如何做到这一点,请帮助我。

Thanks in Advance

我试过了:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
}
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
    if (app.applicationState == UIApplicationStateInactive )
    {
        NSLog(@"app not running");
    }
    else if(app.applicationState == UIApplicationStateActive )
    {
        NSLog(@"app running");
    }
}

我就是这样创建一个本地通知的,它被安排在代码运行当天的17:00。一旦它被触发,方法-(void)application:didReceiveLocalNotification:将被调用。

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[dateComponents setHour:17];
[dateComponents setMinute:00];
[dateComponents setSecond:00];
NSDate *notificationDate = [calendar dateFromComponents:dateComponents];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"blah blah blah";
localNotif.alertAction = @"Ok";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

在2种情况下收到通知

  1. application:didFinishLaunchingWithOptions:方法中,如果应用程序是
  2. application:didReceiveLocalNotification:方法,如果应用程序是要么在运行,要么在后台。展示几乎是没有用的当应用程序已经运行时发出警报。所以你必须显示警报只有当应用程序在后台时才会发出通知解雇。要知道应用程序是否正在从后台恢复,使用applicationWillEnterForeground:方法。
l

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];  
            if (localNotif) {       
                // Show Alert Here
            }
    }

相关内容

  • 没有找到相关文章

最新更新