简体   繁体   English

Swift-如何在后台获取某些静默通知,在前台获取其他静默通知?

[英]Swift -How to get certain silent notifications in the background and others in the foreground?

In my app users can send each other chat messages and place orders for things (similar to OfferUp). 在我的应用程序中,用户可以互相发送聊天消息并下订单(类似于OfferUp)。 I use silent push notifications and I receive them with no problem. 我使用无提示推送通知,但我没有收到任何问题。

What I want to do is have it so when an userA send userB an order, a silent notification will come through wether userB is in the background or foreground . 我想做的就是拥有它,这样当userA向userB发送订单时,无论userB在background还是在foreground都会通过静默通知。

But when userA sends userB a message, the silent notification should only come through if userB is in the background only. 但是,当userA向userB发送消息时,仅当userB仅在background时才应通过静默通知。 There's no need for the notification if userB is in the foreground. 如果userB在前台,则不需要通知。

I use an orderId for the order and messageId for the messages. 我将orderId用于订单,将messageId用于消息。

I get the the silent notifications in App Delegate's : 我在App Delegate's收到静默通知:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    let userInfo = notification.request.content.userInfo

    // I can parse userInfo and get the messageId or the orderId

    guard let userInfo = userInfo as? [String: Any] else { return }

    if let orderId = userInfo["orderId"] as? String {
    }

    if let messageId = userInfo["messageId"] as? String {
    }
}

OrderVC: OrderVC:

func orderWasPlacedNowSendNotification() {

    let urlString = "https://fcm.googleapis.com/fcm/send"

    var apsDict = [String: Any]()
    apsDict.updateValue(title, forKey: "title")
    apsDict.updateValue(body, forKey: "body")
    apsDict.updateValue(1, forKey: "content-available")

    var dataDict = [String: Any]()
    dataDict.updateValue(orderId, forKey: "orderId") // orderId

    var paramDict = [String: Any]()
    paramDict.updateValue(apsDict, forKey: "notification")
    paramDict.updateValue(toToken, forKey: "to")

    paramDict.updateValue(dataDict, forKey: "data")

    let request = NSMutableURLRequest(url: url as URL)
    URLSession.shared.dataTask(with: request as URLRequest) ...
}

MessageVC: MessageVC:

func messageWasSentNowSendNotification() {

    let urlString = "https://fcm.googleapis.com/fcm/send"

    var apsDict = [String: Any]()
    apsDict.updateValue(title, forKey: "title")
    apsDict.updateValue(body, forKey: "body")
    apsDict.updateValue(1, forKey: "content-available")

    var dataDict = [String: Any]()
    dataDict.updateValue(messageId, forKey: "messageId") // messageId

    var paramDict = [String: Any]()
    paramDict.updateValue(apsDict, forKey: "notification")
    paramDict.updateValue(toToken, forKey: "to")

    paramDict.updateValue(dataDict, forKey: "data")

    let request = NSMutableURLRequest(url: url as URL)
    URLSession.shared.dataTask(with: request as URLRequest) ...
}

How can I separate the silent notifications? 如何分隔静默通知?

According to your logic you need to return inside willPresent 根据您的逻辑,您需要在willPresent内部返回

if application.applicationState == .active {   
    completionHandler([])  // no alert/sound
}
else if application.applicationState == .background  { 
     completionHandler([.sound,.alert]) // alert with sound
} 

you can add more checks as you need but last to either return any of the completions for slient/alert respectively 您可以根据需要添加更多检查,但最后一次分别返回任何完成以进行倾斜/警报

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

相关问题 如何在iOS的UAPushNotificationDelegate上捕获后台无声推送通知? - How to capture background silent Push Notifications on UAPushNotificationDelegate for iOS? Swift:如何在后台运行GET调用,然后显示通知 - Swift : How to run a GET call in background and then show notifications 静默推送通知仅在设备正在充电和/或应用程序处于前台时传送 - Silent push notifications only delivered if device is charging and/or app is foreground Swift & FCM:从后台切换到前台时如何获取推送通知数据? - Swift & FCM : How to get push notification data when switching from Background to Foreground? Swift 3如何覆盖静音模式并从背景播放音频 - Swift 3 how to override silent mode and play audio from background 静默的iCloud更改了后台未收到的通知 - Silent iCloud changed notifications not received in background iOS 静默通知的可靠性如何? - How reliable are iOS silent notifications? 快速检测应用程序是在后台还是前台 - Detect if the application in background or foreground in swift Swift:计时器在前台/后台运行 - Swift: Timer run in foreground/background Firebase 推送通知与 cordova 前景和背景 n - Firebase push notifications with cordova foreground and background n
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM