Swift 没有生成(或接收?)我期望的所有通知
我编写了一个简单的应用程序来接收BLE广告,并为每个收到的广告生成一个通知。问题是我没有看到我收到的每个BLE数据包的通知 设备设置直接影响显示内容。这是我在iPhone上设置的:Swift 没有生成(或接收?)我期望的所有通知,swift,nsnotificationcenter,Swift,Nsnotificationcenter,我编写了一个简单的应用程序来接收BLE广告,并为每个收到的广告生成一个通知。问题是我没有看到我收到的每个BLE数据包的通知 设备设置直接影响显示内容。这是我在iPhone上设置的: iOS 12.4.1 常规->通知->应用程序名称-> 允许通知:在 警报:锁定、通知中心和横幅 横幅样式:临时 显示预览:始终 分组:关闭 应用程序代表: public func centralManager(_ central: CBCentralManager, didDiscover peripheral
- iOS 12.4.1
- 常规->通知->应用程序名称->
- 允许通知:在
- 警报:锁定、通知中心和横幅
- 横幅样式:临时
- 显示预览:始终
- 分组:关闭
public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
guard let serviceData = advertisementData["kCBAdvDataServiceData"] as? [CBUUID:Data] else {
return
}
// get the message-specific payload
guard let payload = serviceData[CBUUID(string: "FDF4")] else {
return
}
// Try to parse this advertisement.
let parseResults = self.parsingService.parseData(payload)
// validate this packet.
if self.parsingService.isParseError(parseResults: parseResults, rawBytes: payload.hexEncodedString()) {
return
}
// the packet looks good. Convert it to our internal format
let newPacket = OEMRecord(parseApiResults: parseResults)
// filter out a specific message type to reduce chatter
if (newPacket.getMsgTypeAsInt() == kMessageTypeToListenFor) {
let serialNumber = newPacket.serialNo
if (serialNumber == kSerialNumberToListenFor) {
// we found the device we want!
let msg = "Found tracker: \(serialNumber)"
self.logMessage(msg: msg)
DispatchQueue.main.async { [weak self] in
// we have received a tracker pkt. Tell the entire app (i.e. pass msg to ViewController)
NotificationCenter.default.post(name: NSNotification.Name(didReceiveDesiredTracker), object: self, userInfo: ["text": msg])
}
}
}
}
视图控制器
override func viewDidLoad() {
super.viewDidLoad()
// Request permission to send notifications
notificationCenter.requestAuthorization(options:[.alert, .badge]) { (granted, error) in }
NotificationCenter.default.addObserver(self,
selector: #selector(processReceiveDesiredTrackerNotification(notification:)),
name: NSNotification.Name(rawValue: didReceiveDesiredTracker),
object: nil)
}
// log the incoming BLE msgs to the console & the GUI
@objc func processReceiveDesiredTrackerNotification (notification: NSNotification) {
guard let payload = notification.userInfo else { return } // sanity chk - probably unnecesary
guard let msg = payload["text"] as? String else { return } // ensure we have a payload to display
// log to console & GUI
self.logMessage(msg: msg)
// generate a Local Notification
self.scheduleNotification(msg: msg)
}
// show a (Local) Notification for the pkt just received
private func scheduleNotification(msg: String) {
UIApplication.shared.applicationIconBadgeNumber += 1
let content = UNMutableNotificationContent()
content.title = "ALARM #\(UIApplication.shared.applicationIconBadgeNumber)"
content.body = "\(msg) #\(UIApplication.shared.applicationIconBadgeNumber)"
content.badge = UIApplication.shared.applicationIconBadgeNumber as NSNumber // keep running count of # of pkts received
let request = UNNotificationRequest(identifier: "BLEScannerNotification", content: content, trigger: nil)
notificationCenter.add(request)
}
BLE分组以每次突发5-7个完整分组的突发方式到达。突发中的数据包间隔约0.5秒。每一次爆炸间隔几分钟
1) 在控制台中,我看到接收到一个日志消息
2) 但是,我每次突发只收到1个通知。似乎NotificationCenter处理第一个通知,而忽略(或汇总)每个突发中的其余通知
我错过了什么