您可能遇到以下问题if (applicationIsActive)
健康)状况。
打个断点-didReceiveRemoteNotification
看看它是否在不同的场景中执行,看看它是否在if-健康)状况。
(在一定程度上无关但值得检查) 这个问题:
在后台时的 didReceiveRemoteNotification https://stackoverflow.com/q/5056689/2857130
Note:
-didReceiveRemoteNotification
如果您的应用程序是(最初)关闭,您单击推送通知以打开应用程序。
当应用程序位于前台时或应用程序从后台转换到前台时收到推送通知时,将执行此方法。
苹果参考:https://developer.apple.com/documentation/uikit/uiapplicationdelegate https://developer.apple.com/documentation/uikit/uiapplicationdelegate
如果应用程序正在运行并收到远程通知,则应用程序
调用该方法来处理通知。您的实施
此方法应该使用通知来参加适当的课程
的行动。
...
如果推送通知到达时应用程序未运行,则该方法
启动应用程序并在中提供适当的信息
启动选项字典。应用程序不会调用此方法来处理
那个推送通知。相反,您的实施应用程序:willFinishLaunchingWithOptions: or
应用程序:didFinishLaunchingWithOptions:方法需要得到
推送通知有效负载数据并做出适当响应。
所以...当应用程序不运行并收到推送通知,当用户单击推送通知时,应用程序将启动并now...推送通知内容将在-didFinishLaunchingWithOptions:
其中的方法launchOptions
范围。
换句话说...-didReceiveRemoteNotification
这次不会执行,您还需要执行以下操作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//...
NSDictionary *userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if(apsInfo) {
//there is some pending push notification, so do something
//in your case, show the desired viewController in this if block
}
//...
}
另请阅读Apple 关于处理本地和远程通知的文档 https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/index.html#//apple_ref/doc/uid/TP40008194-CH103-SW4