通过推送通知检测应用程序是否已启动/打开

是否可以通过推送通知了解应用程序是否已启动/打开?


我猜发射事件可以在这里找到:


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


    if (launchOptions != nil) {

         // Launched from push notification

         NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];


    }

}

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


森林海
浏览 649回答 3
3回答

幕布斯7119047

请参阅以下代码:- (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)application:(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");}

慕码人2483693

应用启动后,我们无法正确更新视图。这里有复杂的生命周期方法序列,令人困惑。生命周期方法我们对iOS 10的测试揭示了针对各种情况的以下生命周期方法序列:DELEGATE METHODS CALLED WHEN OPENING APP  Opening app when system killed or user killed      didFinishLaunchingWithOptions      applicationDidBecomeActive    Opening app when backgrounded      applicationWillEnterForeground      applicationDidBecomeActive  DELEGATE METHODS WHEN OPENING PUSHOpening push when system killed    [receiving push causes didFinishLaunchingWithOptions (with options) and didReceiveRemoteNotification:background]    applicationWillEnterForeground    didReceiveRemoteNotification:inactive    applicationDidBecomeActiveOpening push when user killed    didFinishLaunchingWithOptions (with options)    didReceiveRemoteNotification:inactive [only completionHandler version]    applicationDidBecomeActiveOpening push when backgrounded    [receiving push causes didReceiveRemoteNotification:background]    applicationWillEnterForeground    didReceiveRemoteNotification:inactive    applicationDidBecomeActive问题好的,所以现在我们需要:确定用户是否通过推送打开应用根据推送状态更新视图清除状态,以免随后的打开不会使用户回到同一位置。棘手的一点是,必须在应用程序实际处于活动状态时更新视图,这在所有情况下都是相同的生命周期方法。我们的解决方案示意图以下是我们解决方案的主要组成部分:将notificationUserInfo实例变量存储在AppDelegate上。设置notificationUserInfo = nil在这两个applicationWillEnterForeground和didFinishLaunchingWithOptions。设置notificationUserInfo = userInfo在didReceiveRemoteNotification:inactive从applicationDidBecomeActive总是调用自定义方法openViewFromNotification并传递self.notificationUserInfo。如果self.notificationUserInfo为nil,则提早返回,否则根据中的通知状态打开视图self.notificationUserInfo。说明当通过推送打开didFinishLaunchingWithOptions或applicationWillEnterForeground总是在紧接之前调用时didReceiveRemoteNotification:inactive,因此我们首先在这些方法中重置notificationUserInfo,以确保没有过时的状态。然后,如果didReceiveRemoteNotification:inactive调用了,我们就知道我们要从推送中打开,因此我们将self.notificationUserInfo其设置为拾取,然后applicationDidBecomeActive将用户转发到正确的视图。最后一种情况是用户是否在应用程序切换器中打开了该应用程序(即,在应用程序处于前台时双击主屏幕按钮),然后接收到推送通知。在这种情况下,仅会didReceiveRemoteNotification:inactive被调用,而WillEnterForeground和didFinishLaunching都不会被调用,因此您需要一些特殊的状态来处理这种情况。希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS