简体   繁体   English

无法访问时本地通知,并且应用程序在后台

[英]local notification when not reachable and app in background

Goal is to notify end user about reachability changed when app is in background. 目标是在应用程序处于后台时通知最终用户有关可达性已更改的信息。

Broadcasting approach NSNotificationCenter not working in background. 广播方法NSNotificationCenter在后台不起作用。 second approach i tried beginBackgroundTaskWithExpirationHandler in applicationDidEnterBackground but associated issue with it: 第二种方法我试过beginBackgroundTaskWithExpirationHandlerapplicationDidEnterBackground,但与之相关的问题:

a) Time limitation of 10 min a)时间限制为10分钟

b) i calling a end loop for detecting the reachability state which run on main thread and in result it choke the UI. b)我调用结束循环来检测在主线程上运行的可达性状态,结果导致UI阻塞。 Code:- 码:-

-(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;
    });
    }
}

Please suggest me is it possible to notify user in iOS whenever reachability changed and app in background state? 请建议我,只要可访问性发生变化且应用处于后台状态,是否可以在iOS中通知用户?

If yes then how can i achieve this. 如果是的话,我该如何做到这一点。

Thanks. 谢谢。

You could possibly use the background fetch capability as described in the iOS App Programming Guide to check reach ability and post a local notification if the network is un available. 您可以使用iOS应用程序编程指南中所述的后台获取功能来检查可达能力,并在网络不可用时发布本地通知。

There is no guarantee of how often this will be called - probably in the order of every few minutes or longer - certainly not every few seconds. 无法保证会多久调用一次-可能每隔几分钟或更长时间调用一次-当然不是每隔几秒钟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM