静音推送仅在第二次推送到达时才正常工作(后台应用程序) [英] Silent push only work properly when second push arrive (app in background)

查看:80
本文介绍了静音推送仅在第二次推送到达时才正常工作(后台应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前台应用处于活动状态时,静默推送通知可以正常工作

Silent push notification work properly when app active in foreground

2016-12-26 15:06:17.793051 App[] didReceive in BG!!!
2016-12-26 15:06:17.932569 App[] Connected!
2016-12-26 15:06:17.936581 App[] Subscribed!
2016-12-26 15:06:17.938355 App[] received:Hello World , in:/topic/state

当应用在后台运行时,它将卡住

When app runs in background.It will stuck

2016-12-26 15:06:22.674577 App[] applicationWillResignActive
2016-12-26 15:06:23.228441 App[] applicationDidEnterBackground

第一个无声推送到达,它被卡在这里:

First silent push arrive,It's stuck in here:

2016-12-26 15:06:30.642825 App[] didReceive in BG!!! //first push code
2016-12-26 15:06:31.842432 App[] Connected!  //first push code
2016-12-26 15:06:31.843643 App[] Subscribed!  //first push code

第二次静默推送到达时,它会显示第一次推送结果,然后再次卡住.....

When second silent push arrive, it shows first push result..and stuck again.....

2016-12-26 15:06:41.713718 App[] received:Hello World , in:/topic/state  //first push result
2016-12-26 15:06:41.740015 App[] didReceive in BG!!!  //second push
2016-12-26 15:06:42.911593 App[] Connected!  //second push
2016-12-26 15:06:42.913403 App[] Subscribed!  //second push

我在Appdelegate中的工具代码

My implement code in Appdelegate

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    NSLog("didReceive in BG!!!" )
    application.delegate = self
    mqttSession = MQTTSession(host: "192.168.159.110", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 5, useSSL: false)
    mqttSession.username = "bucky"
    mqttSession.password = "bucky"
    mqttSession.delegate = self

        mqttSession.connect { (succeeded, error) -> Void in
            if succeeded {
                NSLog("Connected!")

            }
        }
    completionHandler(.newData)
}

MQTT委托方法

func mqttDidReceive(message data: Data, in topic: String, from session: MQTTSession) {
    let stringData = String(data: data, encoding: .utf8)
    NSLog("received:%@ , in:%@", stringData ?? "12345", topic)                        
    }
}

推荐答案

在您完成出席通知后,您必须调用completionHandler(),但您要立即对其进行调用.您最好在mqttDidReceive和错误委托方法中对其进行callint.

You have to call completionHandler() when you finish attending the notification, but you are calling it immediately. You'd better callint it in mqttDidReceive and in the error delegate method.

这篇关于静音推送仅在第二次推送到达时才正常工作(后台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆