我有一个推送通知,我发送给用户,我希望能够在点击它时采取行动。我知道如果应用程序位于前台,后台,或者用户点击通知中心的警报,则在应用程序委托中调用以下方法:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
但是,如果应用程序未启动且用户在通知到达后立即点击通知横幅,则此方法似乎不会被调用。在这种情况下,我需要采用不同的方法吗?是否应该实施其他方法?
答案 0 :(得分:4)
如果您在点击通知横幅时未启动应用,那么您将在application:didFinishLaunchingWithOptions:
中收到一个NSDictionary。
然后你可以做这样的事情:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *pushDict = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushDict)
{
[self application:application didReceiveRemoteNotification:pushDict];
}
}
此外,在您的application:didReceiveRemoteNtification:
方法中,您可以在收到通知时测试您的应用是否处于非活动状态,如下所示:
-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo
{
if([app applicationState] == UIApplicationStateInactive)
{
NSLog(@"Received notifications while inactive.");
}
else
{
NSLog(@"Received notifications while active.");
}