iOS 在后台推送通知操作
iOS push notification action in background
我对推送通知有疑问。当有人点击通知时,我想调用一些方法。
当应用不是 运行 时,它很简单 (application:didFinishLaunchWithOptions call method
)。但是当应用程序在后台 运行 时,我必须在点击后调用方法,而不是在收到通知后调用方法 (didReceiveRemoteNotification
)。
我不知道该怎么做。
有什么方法可以在应用程序处于后台时检查是否点击了通知?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions)
{ //launchOptions is not nil
NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
userInfoDic = userInfo;
if (apsInfo)
{ //apsInfo is not nil
[self performSelector:@selector(postNotificationToPresentPushMessagesVC)
withObject:nil
afterDelay:1];
}
}
return YES;
}
-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *) userInfo
{
UIApplicationState state = [app applicationState];
if (state == UIApplicationStateBackground)
{
[self postNotificationToPresentPushMessagesVC];
}
else
{
...
}
}
-(void)postNotificationToPresentPushMessagesVC
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"recievePush" object:userInfoDic];
}
我对推送通知有疑问。当有人点击通知时,我想调用一些方法。
当应用不是 运行 时,它很简单 (application:didFinishLaunchWithOptions call method
)。但是当应用程序在后台 运行 时,我必须在点击后调用方法,而不是在收到通知后调用方法 (didReceiveRemoteNotification
)。
我不知道该怎么做。
有什么方法可以在应用程序处于后台时检查是否点击了通知?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions)
{ //launchOptions is not nil
NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
userInfoDic = userInfo;
if (apsInfo)
{ //apsInfo is not nil
[self performSelector:@selector(postNotificationToPresentPushMessagesVC)
withObject:nil
afterDelay:1];
}
}
return YES;
}
-(void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *) userInfo
{
UIApplicationState state = [app applicationState];
if (state == UIApplicationStateBackground)
{
[self postNotificationToPresentPushMessagesVC];
}
else
{
...
}
}
-(void)postNotificationToPresentPushMessagesVC
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"recievePush" object:userInfoDic];
}