iOS 收到无声远程通知后发送本地通知 [英] iOS send a local notification after receiving a silent remote notification

查看:86
本文介绍了iOS 收到无声远程通知后发送本地通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当服务器发送静默通知时,我想检查用户是否处于正确状态,然后如果用户处于正确状态则发出本地通知,或者如果用户不在正确的状态.

When the server sends a silent notification, I want to check if the user is in the right state and then issue a local notification as a result if the user is in the right state or do nothing if the user is not in the right state.

这可行吗?

谢谢!

推荐答案

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

It's not good idea. The system treats background notifications as low-priority. proof: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/pushing_background_updates_to_your_app

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

So background notification is not for you case. If you don't want show notification for some cases, you just in implementation method of UNUserNotificationCenterDelegate don't call completion handler:

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
  }
}

这篇关于iOS 收到无声远程通知后发送本地通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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