【问题标题】:How to get userInfo from Apple Push Notifications如何从 Apple 推送通知中获取 userInfo
【发布时间】:2018-04-19 20:21:21
【问题描述】:

我的企业应用程序正在使用 Apple 推送通知,并且在大多数情况下都能正常工作。我想做的是通过userDidRespondToPush以外的方法从通知中获取用户信息。

如果有通知等待,系统会在 iPhone 主屏幕上的图标上放置一个标记。如果用户自己响应通知,应用程序将触发userRespondsToPush。但是,如果用户没有滑动通知并正常启动应用程序,通过点击显示徽章的应用程序图标,我想让应用程序响应,就好像用户确实滑动了通知一样。这意味着我需要以 userDidRespondToPush 以外的方法获取 userInfo,以便应用知道要向用户显示哪些信息。

Push Notifications 对我来说是很新的东西,但我已经很幸运地让它工作了。我只需要这个小功能即可。

【问题讨论】:

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


    【解决方案1】:

    如果您的应用程序支持 iOS 10 及更高版本,那么您应该能够使用 UNUserNotificationCenter 类检索待处理的通知。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [[UNUserNotificationCenter currentNotificationCenter] getPendingNotificationRequestsWithCompletionHandler:^(NSArray<UNNotificationRequest *> * _Nonnull requests) {
            for (UNNotificationRequest *request in requests) {
                NSDictionary *userInfo = request.content.userInfo;
                NSLog(@"push user info: %@", userInfo);
                // Handle the payload here.
            }
        }];
        return YES;
    }
    

    【讨论】:

    • 我正在查看UNUserNotificationCenter,但我无法让它工作。这显然比我预想的要复杂。我会试一试,然后告诉你。谢谢!
    • 尝试将代码粘贴到 AppDelegate 的 didFinishLaunchingWithOptions: 方法中,并查看用户信息有效负载对象是否正在记录到 lldb 控制台中。我过去使用过这个 API,实现起来非常简单。
    【解决方案2】:

    我猜你想知道从远程通知启动的应用程序还是从应用程序图标正常启动。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        NSDictionary *remoteNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    
        return YES;
    }
    

    如果存在remote Notification,则表示app是从RemoteNotification启动的,否则NO。

    【讨论】:

      【解决方案3】:

      以下是对类似问题的简短回答:如何在除didReceiveRemoteNotification 之外的其他委托方法中获取userInfo。这取决于您的委托方法,但基本上您必须将其挖掘出来:

      您应该将notificationresponseuserInfo 传递给您,具体取决于调用的委托方法。

      一旦您获得userInfo,您就可以根据服务器发送给您的字典在这些委托方法中执行您想要的任何逻辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多