繁体   English   中英

iOS 收到静默远程通知后发送本地通知

[英]iOS send a local notification after receiving a silent remote notification

当服务器发送静默通知时,我想检查用户是否在正确的 state 中,然后如果用户在正确的 state 中则发出本地通知,如果用户不在正确的 Z9ED39E2A691586B73EZ8 中,则不执行任何操作。

这可行吗?

谢谢!

如果您发送带有"content-available": 1的推送通知,它将是静默推送(请参阅 Apple 的本地和远程通知指南)。

然后,您希望使用UNUserNotificationCenter.current().add()有条件地安排本地通知并实现UNUserNotificationCenterDelegate的委托方法。

这不是个好主意。 系统将后台通知视为低优先级。 证明: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app

所以后台通知不适合你的情况。 如果您不想在某些情况下显示通知,您只需在UNUserNotificationCenterDelegate的实现方法中不要调用完成处理程序:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
guard self.shouldShowNotification(notification) else { return } // don't call completion handler if we're don't want to show push
completionHandler(.alert)
}

func shouldShowNotification(_ notification: UNNotification) -> Bool {
  if ... {
    return true
  } else {
    return false
  }
}

暂无
暂无

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

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