【问题标题】:WebServices Call when application in Background and show the response in Local NotificationWebServices 在后台应用程序时调用并在本地通知中显示响应
【发布时间】:2014-08-08 19:50:37
【问题描述】:

我有一个想要继续运行的企业应用程序,因此它可以调用 Web 服务并通过本地通知通知用户。

所以,它现在在后台运行,它发出调用、获取结果、通知用户。 我在 applicationDidEnterBackground 中使用了一个计时器。我的代码是,

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    NSLog(@"ENTER BACKGROUND");
    if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground)
    {
        backgroundTimer=nil;
        [backgroundTimer invalidate];
        backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self  selector:@selector(backgroundTasks) userInfo:nil repeats:YES];
    }

    application.applicationIconBadgeNumber = 0;
}

当定时器被触发时,我会检查日期条件。如果日期匹配,那么我必须进行网络服务调用。

-(void)backgroundTasks
{
    NSDate *CurrentDate = [NSDate date];
    NSDate *previousDate = [[NSUserDefaults standardUserDefaults]valueForKey:@"ScheduledTime"];

    NSLog(@"Current Date : %@ - Previous Date : %@",CurrentDate,previousDate);
    NSString *CurrentdateString = [NSString stringWithFormat:@"%@",CurrentDate];
    NSString *PreviousdateString = [NSString stringWithFormat:@"%@",previousDate];
    NSLog(@"Current Date String : %@ - Previous Date String : %@",CurrentdateString,PreviousdateString);

    if ([PreviousdateString isEqualToString:CurrentdateString])
    {
        NSLog(@"Both dates are same");
        previousDate = [previousDate dateByAddingTimeInterval:60];
        [[NSUserDefaults standardUserDefaults] setObject:previousDate forKey:@"ScheduledTime"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        [self updateUserLatLong:str_Latitude :str_Longitude]; //Webservices call
    }
    else
    {
        NSLog(@"Dates are different");
    }
}

我得到了这个请求的响应,届时我将创建本地通知。像这样,

 str_Condition = [NSString stringWithFormat:@"%@",[conditionarray lastObject]];
                        str_TempFaren = [NSString stringWithFormat:@"%@",[mutArr_High lastObject]];
                        NSLog(@"Condition : %@, Temperature : %@",str_Condition,str_TempFaren);

                        NSLog(@"SHOW ALERT NOTIFICATION");
                        NSLog(@"-----------------------------------------------------------------------");


                        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
                        localNotif.fireDate = [NSDate date];
                        localNotif.timeZone = [NSTimeZone defaultTimeZone];
                        localNotif.alertBody = [NSString stringWithFormat:@"%@° F - %@, See today's recommendations",str_TempFaren,str_Condition];
                        localNotif.alertAction = @"Reminder";

                        localNotif.soundName = UILocalNotificationDefaultSoundName;
                        localNotif.applicationIconBadgeNumber =0;
                        [[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];

我在另一个类中创建默认通知,像这样。

 pickerDate = [datePicker date];
    // Schedule the notification
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = pickerDate;
    localNotification.alertBody = @"See today's recommendations";
    localNotification.alertAction = @"Reminder";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.repeatInterval = NSMinuteCalendarUnit;

//    localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

我的问题是,当用户从内存中删除应用程序时,显然我们无法进行任何 Web 服务调用。因此,当应用程序从内存中删除时,我想在包含默认警报正文的本地通知中显示消息。现在这两种情况都有效。但我只想显示一个通知。如果应用程序从内存中删除,那么只有我必须显示默认通知。如何处理这个问题,请帮助我。我被这个问题打了 7 个多小时。

【问题讨论】:

  • 您希望多久显示一次通知?一天一次?我会在下一次计划的网络检查之后安排默认通知。如果网络检查成功,则取消默认通知并重新安排 now+24 小时。下次您进行网络检查时,请执行相同的操作。

标签: ios objective-c web-services notifications uilocalnotification


【解决方案1】:

我自己找到了答案。解决方法是,我在方法中创建默认的UILocalNotification,

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

    NSDate *pickerDate = [[NSUserDefaults standardUserDefaults]valueForKey:@"ScheduledTime"];

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = pickerDate;
    localNotification.alertBody = @"See today's recommendations";
    localNotification.alertAction = @"Reminder";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.repeatInterval = NSHourCalendarUnit;
    localNotification.soundName = UILocalNotificationDefaultSoundName;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    application.applicationIconBadgeNumber = 0;
}

所以,当我从内存中删除应用程序时。那时我创建了这个通知。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多