检测应用是否从推送通知启动/打开

是否有可能知道应用程序是否从推送通知启动/打开?

我想这个发布会可以在这里抓到:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (launchOptions != nil) {
         // Launched from push notification
         NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

    }
}

但是,当应用程序在后台时,如何才能检测到它是从推送通知中打开的?


请参阅此代码:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground  )
    {
         //opened from a push notification when the app was on background
    }
}

与...一样

-(void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification

晚但可能有用

当应用程序未运行时

  • (BOOL)应用程序:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  • 叫做 ..

    你需要检查推送通知

    NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (notification) {
        NSLog(@"app recieved notification from remote%@",notification);
        [self application:application didReceiveRemoteNotification:notification];
    } else {
        NSLog(@"app did not recieve notification");
    }
    

    这是一个很老的帖子......但它仍然缺少解决问题的实际办法 (正如各种评论所指出的那样)。

    原始问题是关于通过推送通知来检测应用何时启动 / 打开 ,例如用户点击通知。 没有一个答案实际上涵盖了这种情况。

    原因可以在通知到达时在呼叫流程中看到, application:didReceiveRemoteNotification...

    当接到通知时通知是由用户点击被调用。 正因为如此,用户只需查看UIApplicationState就可以知道用户点击了它。

    此外,您不再需要处理应用程序在application:didFinishLaunchingWithOptions...中的“冷启动”情况application:didFinishLaunchingWithOptions...作为application:didReceiveRemoteNotification...在iOS 9+(也可能是8)中启动后再次调用。

    那么,你怎么知道用户点击是否开始了事件链? 我的解决方案是标记应用程序开始走出背景或冷启动的时间,然后在application:didReceiveRemoteNotification...检查时间application:didReceiveRemoteNotification... 如果它小于0.1s,那么你可以很确定龙头触发了启动。

    Swift 2.x

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
      var wakeTime : NSDate = NSDate()        // when did our application wake up most recently?
    
      func applicationWillEnterForeground(application: UIApplication) {    
        // time stamp the entering of foreground so we can tell how we got here
        wakeTime = NSDate()
      }
    
      func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        // ensure the userInfo dictionary has the data you expect
        if let type = userInfo["type"] as? String where type == "status" {
          // IF the wakeTime is less than 1/10 of a second, then we got here by tapping a notification
          if application.applicationState != UIApplicationState.Background && NSDate().timeIntervalSinceDate(wakeTime) < 0.1 {
            // User Tap on notification Started the App
          }
          else {
            // DO stuff here if you ONLY want it to happen when the push arrives
          }
          completionHandler(.NewData)
        }
        else {
          completionHandler(.NoData)
        }
      }
    }
    

    斯威夫特3

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var wakeTime : Date = Date()        // when did our application wake up most recently?
    
        func applicationWillEnterForeground(_ application: UIApplication) {
          // time stamp the entering of foreground so we can tell how we got here
          wakeTime = Date()
        }
    
      func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    
          // ensure the userInfo dictionary has the data you expect
          if let type = userInfo["type"] as? String, type == "status" {
            // IF the wakeTime is less than 1/10 of a second, then we got here by tapping a notification
            if application.applicationState != UIApplicationState.background && Date().timeIntervalSince(wakeTime) < 0.1 {
              // User Tap on notification Started the App
            }
            else {
              // DO stuff here if you ONLY want it to happen when the push arrives
            }
            completionHandler(.newData)
          }
          else {
            completionHandler(.noData)
          }
        }
    }
    

    我已经在iOS 9+上对这两种情况(应用程序在后台,应用程序未运行)进行了测试,并且它像魅力一样起作用。 0.1s也相当保守,实际值为〜0.002s,所以0.01也很好。

    链接地址: http://www.djcxy.com/p/62609.html

    上一篇: Detect if the app was launched/opened from a push notification

    下一篇: APNS (Apple Push Notification Service) reliability