Iphone presentLocalNotificationNow 在后台应用程序时不会触发警报和声音 [英] Iphone presentLocalNotificationNow not triggering alert and sound while app in background

查看:33
本文介绍了Iphone presentLocalNotificationNow 在后台应用程序时不会触发警报和声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序注册位置更新,运行测试,有时当我在应用程序处于后台时进入一个区域时,我会收到带有声音的警报通知.有时我只在通知中心看到通知,我没有收到任何声音和警报......您可以怎样做才能始终收到声音和警报通知?

I have an App registers for location updates, running tests, sometime when I enter a region while the app is in the background I receive a alarm notification with sound. sometime I only see the notification in notification center, and i did not receive any sound and alert... What can you do to always get the sound and the alert notification ?

这就是我的看法

 UILocalNotification *localNotif = [[UILocalNotification alloc] init];
 localNotif.fireDate             = nil;
 localNotif.hasAction            = YES;
 localNotif.alertBody            = fbName;
 localNotif.alertAction          = @"View";
 localNotif.soundName            = UILocalNotificationDefaultSoundName;

[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];

这是应用委托

- (void)application:(UIApplication *)application didReceiveLocalNotification (UILocalNotification *)notification
{
if (notification) 
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                    message:notification.alertBody
                                                   delegate:self cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];


    [alertView show];

}

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

facebook = [[Facebook alloc] initWithAppId:kAppId andDelegate:self];

UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if (notification) 
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert"
                                                        message:notification.alertBody
                                                       delegate:self cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];


    [alertView show];

}



return YES;
}

推荐答案

如果应用程序在后台运行,本地通知将不会收到警报或声音,因为它是由您的应用程序直接接收的.在这种情况下,您需要使用 presentLocalNotificationNow 呈现通知.

If the application is running in the background, the local notification will not get an alert or sound, as it is directly received by your application. In that case, you need to present the notification using presentLocalNotificationNow.

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplicationState applicationState = application.applicationState;
    if (applicationState == UIApplicationStateBackground) {
        [application presentLocalNotificationNow:notification];
    }
}

这篇关于Iphone presentLocalNotificationNow 在后台应用程序时不会触发警报和声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆