如果应用程序已在后台运行,如何响应推送通知视图
我想做一些相当简单的事情。我将一条自定义数据附加到我处理的一些推送通知中,
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
我查找 UIApplicationLaunchOptionsRemoteNotificationKey ,嘿,它就在那里。
仅当我的应用程序首次启动时才会调用该方法。如果当通知到来并且用户按下通知上的“查看”按钮时我的应用程序已经在后台运行,我如何读取相同的键?我想将它们发送到一个特定的视图控制器,并在其上打开该数据,就像我第一次从通知启动应用程序时所做的那样。
I have something fairly simple I want to do. I attach a custom piece of data to some push notifications that I handle in
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I look for the UIApplicationLaunchOptionsRemoteNotificationKey and hey presto there it is.
That method only gets called if my app is being launched for the first time. How do I read that same key if my application is running in the background already when the notification comes in and the user pressed the 'View' button on the notification? I want to send them to a particular view controller with that data open on it, the same as I do if the app is being launched for the first time from the notification.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要检测应用程序是否是通过远程通知激活的,请尝试以下操作:
To detect if the app was activated by remote notication, try this:
查看 iOS 7 及更高版本中的
application:didReceiveRemoteNotification:fetchCompletionHandler:
。如果您的应用程序在前台运行,则会调用
application:didReceiveRemoteNotification:
方法。如果您的应用程序在后台运行并且用户参与您的推送通知(从而使您的应用程序处于活动状态),也会调用它。因此,真正的问题是如何确定应用程序是否位于前台,或者是否由用户参与推送通知而使其处于活动状态。
它看起来像问题的这个答案在后台时的didReceiveRemoteNotification有关键:
您可以判断您的应用程序是否刚刚在
应用程序中进入前台: didReceiveRemoteNotification:
使用这段代码:Check out
application:didReceiveRemoteNotification:fetchCompletionHandler:
in iOS 7 and later.The method
application:didReceiveRemoteNotification:
is called if your app is running in the foreground. It also is called if your app is running in the background and the user engages with your push notification (thus making your app active).So, the real question is how to determine if the app was in the foreground or if it was made active by the user engaging with your push notification.
It looks like this answer to the question didReceiveRemoteNotification when in background has the key:
You can tell whether your app was just brought to the foreground or not in
application:didReceiveRemoteNotification:
using this bit of code: