ios - Iphone presentLocalNotificationNow 在后台应用程序时不会触发警报和声音
<p><p>我有一个应用程序注册位置更新、运行测试,有时当我在应用程序处于后台时进入某个区域时,我会收到带有声音的警报通知。有时我只在通知中心看到通知,并没有收到任何声音和警报...
怎样做才能始终收到声音和警报通知?</p>
<p>这就是我的看法</p>
<pre><code> UILocalNotification *localNotif = [ init];
localNotif.fireDate = nil;
localNotif.hasAction = YES;
localNotif.alertBody = fbName;
localNotif.alertAction = @"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[presentLocalNotificationNow:localNotif];
</code></pre>
<p>这是应用委托(delegate)</p>
<pre><code>- (void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification
{
if (notification)
{
UIAlertView *alertView = [ initWithTitle:@"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
;
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
</code></pre>
<p>{</p>
<pre><code>facebook = [ initWithAppId:kAppId andDelegate:self];
UILocalNotification *notification = ;
if (notification)
{
UIAlertView *alertView = [ initWithTitle:@"Alert"
message:notification.alertBody
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
;
}
return YES;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果应用程序在后台运行,本地通知将不会收到警报或声音,因为它是由您的应用程序直接接收的。在这种情况下,您需要使用 <code>presentLocalNotificationNow</code> 呈现通知。 </p>
<pre><code>- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIApplicationState applicationState = application.applicationState;
if (applicationState == UIApplicationStateBackground) {
;
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - Iphone presentLocalNotificationNow 在后台应用程序时不会触发警报和声音,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/10369945/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/10369945/
</a>
</p>
页:
[1]