【问题标题】:How to get Notification payload in WatchOS2?如何在 WatchOS2 中获取通知负载?
【发布时间】:2016-04-26 10:04:57
【问题描述】:

我在当前的 Watch App 中实现了动态和静态通知。即使在动态通知中发生任何操作时,我也能够在- (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification 方法中接收事件。

  • 现在我担心的是,如何在点击实际打开 WatchOS 应用程序的通知视图时接收任何事件/回调?
  • 如何通过点击通知以任何方式在启动应用时获取推送通知负载?

在动态通知界面的情况下,我使用以下方法来设置 UI。但是,Static 的情况并非如此。

- (void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler { 
    NSLog(@"%@",remoteNotification);
    completionHandler(WKUserNotificationInterfaceTypeCustom);
}

编辑

我在 appdelegate 中添加了以下代码来注册通知。

UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
        [application registerForRemoteNotifications];

【问题讨论】:

    标签: objective-c push-notification apple-push-notifications watchkit watchos-2


    【解决方案1】:

    对于您的 Watch App 的扩展委托,我将研究以下委托方法,以回答您的两个问题:

    - (void)handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)remoteNotification
    

    周长:

    • 标识符:如果应用程序在没有点击通知按钮之一的情况下启动,则等于“”(空NSString),或者如果用户通过点击其中一个启动应用程序,则等于您为通知操作按钮设置的标识符通知的操作按钮。
    • remoteNotification:远程通知附带的有效负载。它采用字典的格式,因此您可以从该对象中获取有效负载。

    问题 1 的答案: 当用户启动应用程序时,如果实现,将调用此方法并允许您根据用户的选择重新配置应用程序。

    问题 2 的答案: 有效负载具有 Apple Notification Payload 字典的传统结构。 aps 键包含alert 信息和badge 信息。然后将您的自定义数据附加到整个字典。要访问它,如果您使用密钥“testKey”配置有效负载字典,则可以使用remoteNotification[@"testKey"] 访问它。

    更新

    根据 Apple 的规定,静态通知“不得包含控件、表格、地图或其他交互元素”,因为它仅在无法及时启动交互通知时用作备用。来源:Apple watchOS Developer Library

    来源和/或其他资源:

    Apple Developer: WK InterfaceController Class Reference

    Apple Developer: The Remote Notification Payload

    Apple Developer: Notification Essentials for watchOS

    【讨论】:

    • 在我的情况下,这个方法在点击通知时没有被调用。
    • @Mrug 您是否按照本文档developer.apple.com/library/ios/documentation/General/… 中的说明通过调用 UIApplication.sharedApplication().registerUserNotificationSettings(settings) 注册了用户通知设置?
    • 我在 appdelegate 中添加了注册代码。也将其发布为问题中的编辑。
    • 我刚刚更新了答案。根据 Apple 的 watchOS 开发者库,静态通知 UI 不应该有任何交互按钮,这就是它不触发任何方法的原因。这能回答你的问题吗?
    • 我没有只使用静态通知,动态和静态都是这种情况。他们都没有提供任何回调亲爱的。
    猜你喜欢
    • 2017-07-23
    • 2020-04-27
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多