Ios 当收到推送通知后应用程序变为活动状态时,在主ViewController上触发事件
我已成功为我的应用程序实施推送通知。 我现在试图完成的是从我的Ios 当收到推送通知后应用程序变为活动状态时,在主ViewController上触发事件,ios,objective-c,apple-push-notifications,Ios,Objective C,Apple Push Notifications,我已成功为我的应用程序实施推送通知。 我现在试图完成的是从我的AppDelegate向根ViewController发送某种标志,以防应用程序收到PN 我首先在我的应用程序IDBecomeActive:中检查徽章编号,如下所示: if (application.applicationIconBadgeNumber>0) { self.hasNotification = YES; NSLog(@"APNs Message received");
AppDelegate
向根ViewController
发送某种标志,以防应用程序收到PN
我首先在我的应用程序IDBecomeActive:
中检查徽章编号,如下所示:
if (application.applicationIconBadgeNumber>0) {
self.hasNotification = YES;
NSLog(@"APNs Message received");
}
现在,我不知道如何将此消息传递给我的root
ViewController
,以便触发一个将用户带到其中一个视图的序列。最好的方法是什么 考虑到许多视图控制器可能对该事件感兴趣,这似乎是使用NSNotifications提供的发布/订阅模型的好选择
发布通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyEventName" object:optionalPayload];
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:)
name:@"MyEventName" object:nil];
}
- (void)dealloc
{
//Unsubscribe yourself in dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)handleNotification:(NSNotification *)pNotification
{
NSLog(@"#1 received message = %@",(NSString*)[pNotification object]);
//Perform your segue here
}
订阅通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyEventName" object:optionalPayload];
- (void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:)
name:@"MyEventName" object:nil];
}
- (void)dealloc
{
//Unsubscribe yourself in dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)handleNotification:(NSNotification *)pNotification
{
NSLog(@"#1 received message = %@",(NSString*)[pNotification object]);
//Perform your segue here
}
备选方案:自定义根VC
如果已创建自己的特定于域的容器视图控制器作为根视图控制器,则可以执行以下操作:
- 将事件发送到根视图控制器李>
- 根视图控制器将询问其当前子级/子级是否对事件感兴趣(可能通过标记协议),并传播该事件李>