我需要确定我的应用是否正在直接接收推送(当它在前台时),而不是用户点击通知中心的推送,从而调用我的应用。
问题是在两种情况下都会调用didReceiveRemoteNotification:fetchCompletionHandler :.因此,当调用此方法时,应用程序如何判断在何种情况下调用它?
答案 0 :(得分:1)
我目前在didReceiveRemoteNotification中实现了这一点。希望它是非常自我解释的!
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if ( application.applicationState == UIApplicationStateBackground )
{
//opened from a push notification when the app was on background
}
else if ( application.applicationState == UIApplicationStateInactive )
{
//push was received while inactive
}
else if ( application.applicationState == UIApplicationStateActive )
{
//push was received while in the foreground
}
}
根据需要使用状态。来自Docs
UIApplicationStateActive
该应用正在前台运行,目前正在接收事件。
UIApplicationStateInactive
应用程序在前台运行但未接收事件。这可能是由于中断或应用程序正在转换到后台或从后台转换而发生的。
UIApplicationStateBackground
该应用正在后台运行。