简体   繁体   English

即使应用程序处于后台或终止状态,我该如何采取措施接收远程通知?

[英]How can I do an action on receiving a remote notification even when the app is in background or terminated?

I have used this function: 我已经使用了这个功能:

 func application(_ application: UIApplication,  didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)

but getting no result. 但没有结果。 Also I have added the required capabilities(refer screenshot for this). 另外,我还添加了必需的功能(请参阅此屏幕截图)。 How can I do it? 我该怎么做? Actually I have to send the device's location to the server when the app receives notification. 实际上,当应用收到通知时,我必须将设备的位置发送到服务器。 I am able to do it when the app is in foreground but not when the app is in background or is terminated. 我可以在应用程序处于前台时执行此操作,但在应用程序处于后台或终止时无法执行此操作。 screenshot 屏幕截图

在后台启动异步网络操作之前​​,需要先调用beginBackgroundTask

Objective 目的

Use this code: here type of backgroundUpdateTask variable is NSInteger 使用此代码:此处的backgroundUpdateTask变量类型为NSInteger

- (void) doUpdate 
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [self beginBackgroundUpdateTask];

        NSURLResponse * response = nil;
        NSError  * error = nil;
        //call your api here for location update
        NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];

        // Do something with the result

        [self endBackgroundUpdateTask];
    });
}
- (void) beginBackgroundUpdateTask
{
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
    }];
}

- (void) endBackgroundUpdateTask
{
    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
    self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}

Swift 迅速

func doUpdate() {
        DispatchQueue.global(qos: .default).async(execute: {() -> Void in
            self.beginBackgroundUpdateTask()
            var response: URLResponse? = nil
            var error: Error? = nil
            //call your api here for location update
            let responseData: Data? = try? NSURLConnection.sendSynchronousRequest(request, returning: response)
            // Do something with the result
            self.endBackgroundUpdateTask()
        })
    }

    func beginBackgroundUpdateTask() {
        backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {() -> Void in
            self.endBackgroundUpdateTask()
        })
    }

    func endBackgroundUpdateTask() {
        UIApplication.shared.endBackgroundTask(backgroundUpdateTask)
        backgroundUpdateTask = UIBackgroundTaskInvalid
    }

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

相关问题 应用终止时的远程通知 - Remote notification when app is terminated 当应用程序在后台/终止时如何忽略远程/推送通知 - How to ignore a remote/push notification while app is on background/terminated 当应用程序处于状态背景或被杀死时,如何在不点击通知的情况下存储 iOS 远程通知? - How can i store iOS remote notification when app in state background or killed, and without tap the notification? 如何从远程通知操作按钮打开应用程序 - How can I open app from remote notification action button 应用从后台终止时如何获取来电通知 - How to get incoming call notification when app is terminated from background 当应用未运行(终止)时,通知应用有关远程推送通知的信息 - Notify the app about remote push notification when the app is not running (terminated) 应用终止时处理远程推送通知的声音 - Handling remote push notification sounds when app is terminated 收到远程通知后在后台设置iOS应用徽章 - Setting iOS app badge while in the background after receiving a remote notification 当应用程序在后台或终止时,FCM通知iOS无法正常工作 - FCM Notification iOS not working when app is in background or terminated 如何确定我的应用程序何时即将在后台终止? - How can I determine when my application is about to be terminated in the background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM