【问题标题】:Can we avoid displaying push notification我们可以避免显示推送通知吗
【发布时间】:2019-02-17 23:43:09
【问题描述】:

有一种情况,我需要检查用户是否应该根据他的位置接收推送通知。我知道UNNotificationServiceExtension 帮助我们拦截通知并有助于构建推送内容。但是,对于此类或其他任何地方,我们是否有可能通过检查用户的位置不显示推送通知?

这并不意味着我每次都想这样做。但只有当我的条件不满足时。在通知内容中,我正在获取用户的最后跟踪位置,我将使用该位置与用户的当前位置进行比较。
我浏览了this,但没有得到令人信服的答案。
注意:我不是在寻找静默推送通知解决方案。

【问题讨论】:

    标签: ios apple-push-notifications unnotificationserviceextension


    【解决方案1】:

    使用 UNUserNotificationCenter 您可以通过 id 取消单个通知。

    UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [SomeID])
    

    您必须实施 UNUserNotificationCenterDelegate 并实施 userNotificationCenter(_:willPresent)

    类似:

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let identifier = notification.request.identifier
            let content = notification.request.content
            //your logic here
        }
    

    【讨论】:

    • 推送通知的标识符可能是什么?
    • 对。但是我需要传递的标识符(参数)是什么?我从哪里得到那个?
    • 您可以添加用于构建和显示通知的代码吗?
    • 这是有效载荷:{ aps = { alert = "这就是您的消息的样子!在文本区域中输入您的消息并在此处进行预览";徽章 = 1; “可变内容”= 1;声音=默认; }; “附件网址”=“”;深层链接 = ""; "message_id" = 1793; }
    • 好的,但是你在哪里执行此操作 -> “在通知内容中,我正在获取用户的最后跟踪位置,我将使用它与用户的当前位置进行比较”
    猜你喜欢
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多