要在iOS中接收推送消息,我使用以下方法:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
此方法位于我的AppDelegate.m中。一切都很好。我的问题是,这个方法是否可能不在AppDelegate中而是在其他任何地方?!
答案 0 :(得分:1)
不,这是不可能的,因为这是由iOS决定的。查看文档here。
您可以做的是创建一个专门的类(例如PushNotificationHandler
),应该在那里处理推送通知并在application:didReceiveRemoteNotification:fetchCompletionHandler:
内调用它。
答案 1 :(得分:0)
使用NSNotificationCenter,您可以轻松地广播推送通知:
广播:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSDictionary *userInfo = @{@"localNotification": notification};
[[NSNotificationCenter defaultCenter] postNotificationName:@"DOMyApplicationDidReceiveLocalNotification"
object:notification
userInfo:userInfo];
}
在需要的地方聆听:
[[NSNotificationCenter defaultCenter] addObserverForName:@"DOMyApplicationDidReceiveLocalNotification"
object:nil
queue:nil
usingBlock:^(NSNotification *note)
{
UILocalNotification *localNotification = note.userInfo[@"localNotification"];
//...
}];
NSNotificationCenter提供了多种收听方法,不仅仅是基于块的方式。