gpt4 book ai didi

ios - 在 Appdelegate.m 的后台线程上发送 NSNotification

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:42:05 26 4
gpt4 key购买 nike

我正在接收远程通知。我想发布一个 nsnotification。我可以发布通知,但我无法在我当前的 View Controller 中接收它。

这是我的代码。

我知道这被称为:

AppDelegate.m

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{

[[NSNotificationCenter defaultCenter] postNotificationName:CHAT_MESSAGE_RECEIVED object:nil userInfo:messageIdDict];

}

这会在上面的代码被调用之前被调用

MyViewController.m

 -(void)viewWillAppear:(BOOL)animated
{

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(messageReceived:)
name:CHAT_MESSAGE_RECEIVED
object:self];

}

-(void)messageReceived:(NSDictionary *)userInfo
{
NSLog(@"Logged");
}

最佳答案

下面是我的做法:

在 AppDelegate 中:

- (void)application:(UIApplication *)application 
didReceiveRemoteNotification:(NSDictionary *)userInfo
{

[[NSNotificationCenter defaultCenter] postNotificationName:CHAT_MESSAGE_RECEIVED
object:messageIdDict
userInfo:nil];

}

在 MyViewController 中:

-(void)viewWillAppear:(BOOL)animated
{

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(messageReceived:)
name:CHAT_MESSAGE_RECEIVED
object:nil];

}

-(void)messageReceived:(NSNotification *)notification
{
NSDictionary *userInfo = NSDictionary dictionaryWithDictionary:[notification object]];
NSLog(@"Logged");
}

希望对你有帮助

关于ios - 在 Appdelegate.m 的后台线程上发送 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17609803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com