为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

iOS
森林海 2019-10-15 14:44:42
是否可以通过推送通知了解应用程序是否已启动/打开?我猜发射事件可以在这里找到:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    if (launchOptions != nil) {         // Launched from push notification         NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];    }}但是,当应用程序在后台运行时,如何从推送通知中检测到它已打开?
查看完整描述

3 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

请参阅以下代码:


- (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


查看完整回答
反对 回复 2019-10-15
?
四季花海

TA贡献1811条经验 获得超5个赞

晚了,但也许有用


当应用未运行时


- (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");

}


查看完整回答
反对 回复 2019-10-15
?
慕码人2483693

TA贡献1860条经验 获得超9个赞

应用启动后,我们无法正确更新视图。这里有复杂的生命周期方法序列,令人困惑。


生命周期方法


我们对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 PUSH


Opening push when system killed

    [receiving push causes didFinishLaunchingWithOptions (with options) and didReceiveRemoteNotification:background]

    applicationWillEnterForeground

    didReceiveRemoteNotification:inactive

    applicationDidBecomeActive


Opening push when user killed

    didFinishLaunchingWithOptions (with options)

    didReceiveRemoteNotification:inactive [only completionHandler version]

    applicationDidBecomeActive


Opening 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都不会被调用,因此您需要一些特殊的状态来处理这种情况。


希望这可以帮助。


查看完整回答
反对 回复 2019-10-15
  • 3 回答
  • 0 关注
  • 648 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信