在我的应用程序中,我打算使用本地通知而不是推送通知。我需要检查交易更新是否可用,如果是,只需使用本地通知通知用户。我已经完成了以下步骤。
- 在
didFinishLaunchingWithOptions
中,设置获取间隔[[UIApplication sharedApplication]setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]
。 -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
> 为 POST 请求添加了如下代码以及我的请求数据:
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn) {
completionHandler(UIBackgroundFetchResultNewData);
}
else {
completionHandler(UIBackgroundFetchResultFailed);
}
响应解析后,比较是否有任何更新以及是否有任何更新,使用以下代码显示通知。
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1]; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.alertTitle = @"SIB Mirror"; localNotification.alertBody = @"You have a new transaction. Go to e-Statements to view it."; localNotification.timeZone = [NSTimeZone defaultTimeZone]; [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
问题是大多数时候,当手机被锁定时,收到错误消息 >Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."
因此没有显示通知。但是当我将电话直接连接到电话并在调试模式下启用后台同步时,一切正常。
请指导我一个简单的方法来做到这一点,或者如果我做错了什么,请纠正我。
谢谢,
移动开发者。