【问题标题】:Push Notification Delivery and Dismiss Callback推送通知传递和关闭回调
【发布时间】:2016-03-03 06:06:02
【问题描述】:

当应用不活动时,我们可以在推送通知发送或关闭时从客户端进行服务调用吗?

APNS 是否还提供任何有关通知是否在个人级别传递的信息,是否有任何 API 可以用来查询传递状态报告?

【问题讨论】:

    标签: ios objective-c push-notification apple-push-notifications


    【解决方案1】:

    是的,如果您将content-available 标志发送到aps 字典中的1,您可以检测到远程通知的传递。如果设置了key,就会调用AppDelegate的application:didReceiveRemoteNotification:fetchCompletionHandler:方法。

    此外,从 iOS 10 开始,您还可以检测远程通知的消失。为此,您需要实施 UserNotifications 框架。

    您需要执行以下步骤:

    1. 使用 iOS 10 API 注册远程通知的类别。 您需要检查[[UNUserNotificaionCenter currentNotificationCenter] setNotificationCategories] 方法是否相同。见https://developer.apple.com/documentation/usernotifications/unusernotificationcenter

    2. iOS 10 引入了一个名为 UNNotificationCategory 的新类,用于封装类别实例。您需要在类别实例上设置CustomDismissAction 才能接收解除回调。

    https://developer.apple.com/documentation/usernotifications/unnotificationcategory

    1. 通过userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:方法的实现实现UNUserNotificationCenterDelegate协议。如果您执行上述步骤,此方法将接收对解除操作的回调。

    https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate

    将您的委托设置为 UserNotificationCenter - [[UNUserNotificaionCenter currentNotificationCenter].delegate = your_delegate_implementation

    1. 在负载中使用CustomDismissAction 属性设置类别。
        {
            "aps": {
                "alert": "joetheman",
                "sound": "default",
                "category": "my-custom-dismiss-action-category",
                "content-available":1
            },
            "message": "Some custom message for your app",
            "id": 1234
    }
    

    【讨论】:

    • 我能够使用这个解决方案来解除回调。谢谢!
    • 你写它的方式有点暗示customDismissAction 仅用于解雇目的,但这是不正确的。这只是拥有类别的众多目的之一。类别可用于 1. 分组操作 2. 添加占位符以防用户隐藏预览 3. 通过类别进行其他调整 选项,例如您的答案
    【解决方案2】:

    是的,当应用不活动时收到通知时,您可以从 ios 应用拨打服务电话。这样做:

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        <#code#>
    }
    

    是的,您可以在推送通知负载中添加额外的键值对。

    {
        "aps": {
            "alert": "joetheman",
            "sound": "default"
        },
        "message": "Some custom message for your app",
        "id": 1234
    }
    

    【讨论】:

    • 在用户选择该通知之前不会调用此方法
    • @chiragshah 你需要在 aps 中添加 content-available key 到 1 以触发交付方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-13
    相关资源
    最近更新 更多