当App处于后台模式时,UILocalNotification可以触发自定义方法吗?

Jor*_*mos 9 iphone notifications objective-c

好吧标题是自我解释.我想创建一个App,可以在App处于后台模式时管理编程的本地通知.通知工作顺利但我想在触发警报时触发自定义方法.

可能吗?谢谢.

iPh*_*Dev 6

是的,可以做到.你可以这样做:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [NSTimer scheduledTimerWithTimeInterval:17.0 target:self selector:@selector(makeNotificationRequest:) userInfo:nil repeats:YES];
}

-(void)makeNotificationRequest:(NSTimer *)timer
{
    CLLocation *location = [[AppHelper appDelegate] mLatestLocation];
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc] init];

#ifdef _DEBUG
    [paramDic setValue:[NSString stringWithFormat:@"77.586"] forKey:@"Lat"];
    [paramDic setValue:[NSString stringWithFormat:@"12.994"] forKey:@"long"];
#else
    [paramDic setValue:[NSString stringWithFormat:@"%f",location.coordinate.latitude] forKey:@"Lat"];
    [paramDic setValue:[NSString stringWithFormat:@"%f",location.coordinate.longitude] forKey:@"long"];
#endif

    WNetwork *mNetwork = [[WNetwork alloc] init];
    [mNetwork makeRequsetWithURL:URL_Showbeeps type:JBJsonParser paramDictionary:paramDic delegate:self];
    [mNetwork autorelease];
    NSLog(@"URL HIT%@",paramDic);
    [paramDic autorelease];
}
Run Code Online (Sandbox Code Playgroud)

要根据警报自定义您的操作,您可以使用以下命令:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    UIApplicationState state = [application applicationState];
    if (state == UIApplicationStateActive) {
        ;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 据记载,当应用程序进入后台时,计时器停止滴答作响 (3认同)

Ben*_*jie 1

如果应用程序位于前台,那么您需要 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification以下方法UIApplicationDelegate,但是文档表明,当应用程序位于后台时,不会调用该方法。

scheduledLocalNotifications您可以通过拨打电话获取本地通知列表UIApplication- 然后您可以轮询这些通知的时间并安排一个函数在后台模式下调用。虽然这不一定与本地通知触发时 100% 匹配,但我认为它与 App Store 指南所允许的最接近。

当您处于后台模式时,您还可以通过调用presentLocalNotificationNow:方法来呈现您自己的本地通知:

https://developer.apple.com/library/IOS/#documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/presentLocalNotificationNow

因此,您可以通过仅呈现您自己的通知而不是安排操作系统发送通知来解决此问题。

如果您尝试从其他应用程序访问本地通知,那么我认为这是不允许的。