[英]local notification when not reachable and app in background
目标是在应用程序处于后台时通知最终用户有关可达性已更改的信息。
广播方法NSNotificationCenter在后台不起作用。 第二种方法我试过beginBackgroundTaskWithExpirationHandler在applicationDidEnterBackground,但与之相关的问题:
a)时间限制为10分钟
b)我调用结束循环来检测在主线程上运行的可达性状态,结果导致UI阻塞。 码:-
-(void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"Application entered background state.");
// bgTask is a property of the class
if (isUserLogin) {
NSAssert(self.bgTask == UIBackgroundTaskInvalid, nil);
bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_current_queue(), ^{
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
});
}];
dispatch_async(dispatch_get_current_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0) {
curReach = [[utilities sharedUtility]reachabilityChanged];
NetworkStatus netStatus = [curReach currentReachabilityStatus];
NSLog(@"-----------back ground task------------");
if (netStatus == ReachableViaWWAN || netStatus == ReachableViaWiFi) {
//isNetworkMessagePopup = YES;
}
else{
// if (isNetworkMessagePopup) {
// isNetworkMessagePopup = NO;
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = FORMAT_UNABLE_TO_CONNECT_SERVER_DUE_TO_NO_INTERNET;
localNotif.alertAction = NSLocalizedString(@"Read Message", nil);
localNotif.soundName = @"pushNotification.caf";
localNotif.repeatInterval = 5;
//localNotif.applicationIconBadgeNumber = 1;
[application presentLocalNotificationNow:localNotif];
[localNotif release];
break;
//}
}
}
}
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
});
}
}
请建议我,只要可访问性发生变化且应用处于后台状态,是否可以在iOS中通知用户?
如果是的话,我该如何做到这一点。
谢谢。
您可以使用iOS应用程序编程指南中所述的后台获取功能来检查可达能力,并在网络不可用时发布本地通知。
无法保证会多久调用一次-可能每隔几分钟或更长时间调用一次-当然不是每隔几秒钟。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.