如何在后台处理NSLocal通知?

时间:2016-02-02 04:09:43

标签: ios objective-c uilocalnotification alarm

  • 多个警报通知警报?

  • 如果应用程序在后台运行。

2 个答案:

答案 0 :(得分:0)

AppDelegate.m课程中使用以下方法。

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif{
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

    if (application.applicationState == UIApplicationStateInactive )
    {
        NSLog(@"app not running");
    }
    else if(application.applicationState == UIApplicationStateActive )
    {
        NSLog(@"App is running");

    }else if(application.applicationState == UIApplicationStateBackground){
        NSLog(@"App is running in background");
    }
}

这可能对您在后台处理本地通知很有帮助。

答案 1 :(得分:0)

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notif{
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

    if (application.applicationState == UIApplicationStateInactive )
    {
        NSLog(@"app not running");
    }
    else if(application.applicationState == UIApplicationStateActive )
    {
        NSLog(@"App is running");

    }else if(application.applicationState == UIApplicationStateBackground){
        NSLog(@"App is running in background");
    }
}

除了JAGAT对iOS 8+的回答之外,您还需要申请注册才能收听本地通知。你可以用这个:

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler{

}

并且允许这样做:

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                         settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                                                         categories:nil]];
}