[英]UILocalNotification doesn't trigger didReceiveLocalNotification when tapping from Notification Center
[英]How to call didReceiveLocalNotification:(UILocalNotification *)notification in iPhone?
我需要后台進程(用於調用Web服務)在應用啟動狀態后調用didReceiveLocalNotification:(UILocalNotification *)notification,該怎么做,請幫幫我。
提前致謝
我嘗試了這個:
- (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種情況下收到通知
application:didFinishLaunchingWithOptions:
方法中,如果該應用程序既不在運行又不在后台運行。 application:didReceiveLocalNotification:
方法中,如果應用程序正在運行或在后台運行。 當應用程序已經運行時,幾乎沒有用來顯示警報。 因此,您僅在通知觸發時應用程序處於后台時才顯示警報。 要了解該應用程序是否正在從后台恢復,請使用applicationWillEnterForeground:
方法。 升
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
// Show Alert Here
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.