Objective-C: 如何正确处理 iOS 推送通知?
Objective-C: How to Handle an iOS push notification properly?
我已经在我的应用程序中正确实施了所有内容以接收推送通知,我收到的通知很好,但是当用户点击它时我该如何做呢?
这是我关于此事的代码:
AppDelegate.m:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"Received notification: %@", userInfo);
NSLog(@"Hello from appDelegate");
}
正在运行,我在 Xcode 日志中收到了用户信息和其他消息。
现在我想在用户单击通知时执行某些操作(转到特定的 segue)。我看过 docs 但它非常复杂且难以理解。
我只需要知道在 MainViewController.m
中使用什么函数
有什么提示吗?提前致谢。
您可以 post 来自 AppDelegate
的不同事件的通知(使用 NSNotificationCenter
),然后添加观察者特别是 classes 在任何您想执行特定的地方操作。
代码实现
-(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
}
}
现在,在您的特定 class 中,您可以按如下方式处理通知。
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:@"" selector:@selector(callMyWebService) name:nil object:nil];
}
-(void)callMyWebService{
//Perform your action.
}
我已经在我的应用程序中正确实施了所有内容以接收推送通知,我收到的通知很好,但是当用户点击它时我该如何做呢?
这是我关于此事的代码:
AppDelegate.m:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"Received notification: %@", userInfo);
NSLog(@"Hello from appDelegate");
}
正在运行,我在 Xcode 日志中收到了用户信息和其他消息。
现在我想在用户单击通知时执行某些操作(转到特定的 segue)。我看过 docs 但它非常复杂且难以理解。
我只需要知道在 MainViewController.m
中使用什么函数有什么提示吗?提前致谢。
您可以 post 来自 AppDelegate
的不同事件的通知(使用 NSNotificationCenter
),然后添加观察者特别是 classes 在任何您想执行特定的地方操作。
代码实现
-(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
}
}
现在,在您的特定 class 中,您可以按如下方式处理通知。
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:@"" selector:@selector(callMyWebService) name:nil object:nil];
}
-(void)callMyWebService{
//Perform your action.
}