Objective-C:如何正确处理iOS推送通知?

时间:2015-12-24 12:12:17

标签: ios objective-c iphone apple-push-notifications

我已经在我的应用中正确实现了所有内容以接收推送通知,我收到通知就好了,但是当用户点击它时我该怎么办?

以下是关于此事的代码:

AppDelegate.m:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    NSLog(@"Received notification: %@", userInfo);
    NSLog(@"Hello from appDelegate");
}

它正在工作,我在我的Xcode日志中获取了userInfo和另一条消息。

现在我想在用户点击通知时做某事(转到特定的segue)。我见过docs,但这很复杂,难以理解。

我只需要知道MainViewController.m中要使用的函数

任何提示?提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以在NSNotificationCenter上发布有关不同事件的通知(使用AppDelegate),然后在您想要执行特定操作的特定类中添加观察者。

代码实施

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    NSString *aStrEventType = userInfo[@"eventType"];
    if ([aStrEventType isEqualToString:@"callWebService"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"callWebService" object:nil];
    }else{
        // Implement other notification here
    }
}

现在,在您的特定课程中,您可以按如下方式处理通知。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:@"" selector:@selector(callMyWebService) name:nil object:nil];
}
-(void)callMyWebService{
      //Perform your action.
}