iOS端消息推送如何获取消息内容?
当App在前台时:当App在前台时可以通过以下方式获取App的消息内容:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{ NSDictionary * userInfo = notification.request.content.userInfo;}使用了静默推送方式,可以在下面的方法中获取
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandleruserInfo就是消息内容。
当App在后台时:
使用了静默推送方式,可以在下面的方法中获取
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler当消息在后台时,其他的方法是没有办法获取到消息内容的,只有在点击消息以后,进入到App才可以获取到消息内容,方法如下:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{ NSDictionary * userInfo = response.notification.request.content.userInfo;}userInfo就是消息内容。