如何访问 applicationDidBecomeActive 上的远程推送通知数据?
Posted
技术标签:
【中文标题】如何访问 applicationDidBecomeActive 上的远程推送通知数据?【英文标题】:How do I access remote push notification data on applicationDidBecomeActive? 【发布时间】:2011-07-18 11:48:35 【问题描述】:当应用程序在后台接收到远程推送通知时,应用程序进入 applicationDidBecomeActive。从那里,我如何从通知中访问数据的 NSDictionary?
【问题讨论】:
【参考方案1】:通知数据在application:didReceiveRemoteNotification:
中传送到您的应用程序。如果你想在applicationDidBecomeActive:
中处理它,你应该将它存储在application:didReceiveRemoteNotification:
并在applicationDidBecomeActive
中再次读取。
【讨论】:
很抱歉恢复这个,但是当你收到关于 application:didReceiveRemoteNotification 的通知并且你的应用程序在前台时会发生什么,这意味着 applicationDidBecomeActive 将不会被调用。 在application:didReceiveRemoteNotification:
中,您可以查询-[UIApplication applicationState]
以了解您的应用是否已经在前台。如果是,则立即处理通知。【参考方案2】:
Swift 版本:
var dUserInfo: [NSObject : AnyObject]?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
// code...
if let info = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject]
dUserInfo = info
return true
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])
dUserInfo = userInfo
func applicationDidBecomeActive(application: UIApplication)
// code...
self.yourAction(dUserInfo)
func yourAction(userInfo: [NSObject : AnyObject]?)
if let info = userInfo?["aps"] as? Dictionary<String, AnyObject>
【讨论】:
【参考方案3】:我使用这段代码来管理推送:
在 AppDelegate 中
@implementation AppDelegate
NSDictionary *dUserInfo; //To storage the push data
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
//Check for options
if (launchOptions != nil)
//Store the data from the push.
dUserInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dUserInfo != nil)
//Do whatever you need
return YES;
- (void)applicationDidBecomeActive:(UIApplication *)application
//Data from the push.
if (dUserInfo != nil)
//Do whatever you need
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
//Store the data from the push.
if (userInfo != nil)
dUserInfo = userInfo;
我希望这对某人有用。
编码愉快。
【讨论】:
【参考方案4】:如果您的应用在收到并点击推送通知时处于background
状态,则应用将使用application:didFinishLaunchingWithOptions:
而不是application:didReceiveRemoteNotification:
调用。
可以从launchOptions
字典中的application:didFinishLaunchingWithOptions:
访问推送通知负载。
【讨论】:
This is not correct application:didFinishLaunchingWithOptions: 应用在后台时不调用,仅在应用启动时调用。你所说的完全相反,首先它会被称为 application:didReceiveRemoteNotification: 然后是 applicationDidBecomeActive:以上是关于如何访问 applicationDidBecomeActive 上的远程推送通知数据?的主要内容,如果未能解决你的问题,请参考以下文章