当应用程序进入前台时检测远程通知

Detect Remote Notifications when application enters foreground

美好的一天, 我有一个关于在 iOS 上接收远程通知的问题,我知道当应用程序是 运行 或应用程序不是 运行 时将调用 n-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 并且用户点击通知。

但是,如果用户在锁屏时忽略通知徽章并解锁iPhone,之后你运行应用程序,那么该功能将不会被调用,那么我如何才能收到收到的通知!!

谢谢你:)

您可以使用以下代码执行此操作

 NSDictionary *pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    if(pushNotificationPayload)
    {
        [self application:application didReceiveRemoteNotification:pushNotificationPayload];
    }

对于任何情况,检查用户是否点击过,或者应用程序是否在后台或处于活动状态。 您只需在

中检查应用程序状态
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

    if(application.applicationState == UIApplicationStateActive) {

        //app is currently active, can update badges count here

    }else if(application.applicationState == UIApplicationStateBackground){

        //app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here

    }else if(application.applicationState == UIApplicationStateInactive){

        //app is transitioning from background to foreground (user taps notification), do what you need when user taps here

    }

如果您忽略来自通知中心的通知,那么您将无法在应用程序中获取它。

当 phone 被锁定时,应用程序被 OS 发送到非活动状态。 因此,这段时间内收到的通知与应用程序在后台时收到的通知一样好。 就像在后台收到的任何通知一样,除非用户点击托盘中的通知,否则应用程序无法获得控制权。