【问题标题】:Iphone presentLocalNotificationNow not triggering alert and sound while app in backgroundIphone presentLocalNotificationNow 在后台应用程序时不会触发警报和声音
【发布时间】:2012-04-29 05:30:52
【问题描述】:

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

这就是我的看法

 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;
}

【问题讨论】:

标签: ios uialertview uilocalnotification


【解决方案1】:

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

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

【讨论】:

  • 我认为您不想调用 presentLocalNotificationNow。这是为了立即发布本地通知。我相信你可能会在这里陷入无限循环。
  • presentLocalNotificationNow: 只提供本地通知,不会重新发布。
猜你喜欢
  • 2012-03-11
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多