【问题标题】:Objective-C: How to Handle an iOS push notification properly?Objective-C:如何正确处理 iOS 推送通知?
【发布时间】:2015-12-24 12:12:17
【问题描述】:

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

这是我关于此事的代码:

AppDelegate.m:

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

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

现在我想在用户单击通知时做一些事情(转到特定的 segue)。我看过docs,但它非常复杂且难以理解。

我只需要知道在 MainViewController.m 中使用什么函数

有什么提示吗?提前致谢。

【问题讨论】:

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


【解决方案1】:

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

代码实现

-(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.
}

【讨论】:

  • 请说明您要执行的操作。
  • 我正在发送一个带有 artilce_id 的推送通知,我想获取这个 article_id 并执行特定的 segue
  • 您是否在屏幕上您想从哪里推送 segue?我的意思是确保您不在其他屏幕上,然后想从其他地方推送。
猜你喜欢
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多