ios - 在 Appdelegate.m 的后台线程上发送 NSNotification
<p><p>我正在接收远程通知。我想发布一个 nsnotification。我可以发布通知,但我无法在当前的 ViewController 中接收它。</p>
<p>这是我的代码。</p>
<p>我知道这会被调用:</p>
<p><code>AppDelegate.m</code></p>
<pre><code> - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[ postNotificationName:CHAT_MESSAGE_RECEIVED object:nil userInfo:messageIdDict];
}
</code></pre>
<p>在调用上述代码之前调用它</p>
<p><code>MyViewController.m</code></p>
<pre><code> -(void)viewWillAppear:(BOOL)animated
{
[ addObserver:self
selector:@selector(messageReceived:)
name:CHAT_MESSAGE_RECEIVED
object:self];
}
-(void)messageReceived:(NSDictionary *)userInfo
{
NSLog(@"Logged");
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我会这样做:</p>
<p>在 AppDelegate 中:</p>
<pre><code>- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[ postNotificationName:CHAT_MESSAGE_RECEIVED
object:messageIdDict
userInfo:nil];
}
</code></pre>
<p>在 MyViewController 中:</p>
<pre><code>-(void)viewWillAppear:(BOOL)animated
{
[ addObserver:self
selector:@selector(messageReceived:)
name:CHAT_MESSAGE_RECEIVED
object:nil];
}
-(void)messageReceived:(NSNotification *)notification
{
NSDictionary *userInfo = NSDictionary dictionaryWithDictionary:];
NSLog(@"Logged");
}
</code></pre>
<p>希望对你有帮助</p></p>
<p style="font-size: 20px;">关于ios - 在 Appdelegate.m 的后台线程上发送 NSNotification,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/17609803/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/17609803/
</a>
</p>
页:
[1]