UILocalNotification 不起作用...iPhone 4 iOS 4.3 上没有振动
我会在这里写尽可能多的内容,但标题几乎说明了一切。我在几种不同的情况下测试过它:
1)手机打开,静音模式关闭,应用程序打开,在前台,屏幕解锁
我知道这会通过应用程序委托的 didReceiveLocalNotification,并且除了处理之外不会出现声音或振动我在 didReceiveLocalNotification 下包含的代码。处理代码实际上被调用
NSURL *Sound = [[NSBundle mainBundle] URLForResource: self.currentSoundPVC
withExtension: @"caf"];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
AudioServicesPlayAlertSound (soundFileObject);
并且确实有效!它会同时播放声音并振动手机。
2)手机打开,静音模式关闭,应用程序打开,在后台,屏幕解锁
现在,我在应用程序委托的 applicationDidEnterBackground 中设置了 uilocalnotification 警报,使用以下代码
NSString *Sound = [self.currentSoundPVC stringByAppendingString:@".caf"];
UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
[alarm setSoundName:Sound];
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
,但这仅部分有效!声音可以播放,但没有振动!现在,我认为这是一个错误,因为我引用了苹果开发者网站,
“当发送通知并播放声音时,系统还会在支持它的设备上触发振动“
现在,显然我的 iPhone 不是这种情况,而且模拟器显然不会振动,所以我无法测试。这个问题并希望在开发者社区中得到解决!
3) 手机打开,静音模式关闭,应用程序打开,在前台,屏幕锁定
与 #2 相同
4) 手机打开,静音模式关闭,应用程序打开,在后台,屏幕锁定
与 #2 相同
5) 手机打开,静音模式关闭, app off (后台进程被删除)
与 #2 相同,因为 uilocalnotifications 从未被取消,所以 iOS 仍然认为它们是有效的。
6)手机打开,静音模式打开,应用程序打开,在后台,屏幕解锁
没有声音,没有振动,什么都没有!这太糟糕了!我本来希望苹果能像往常一样拿出一些开箱即用的东西!
I would write as much as I could write here, but the Title pretty much says it all. I've tested it in several different situations:
1) Phone on, silent mode off, app on, in foreground, screen unlocked
I know that this goes through app delegate's didReceiveLocalNotification and didn't expect a sound or vibration, except for the handling code that I included under didReceiveLocalNotification. The handling code actually called
NSURL *Sound = [[NSBundle mainBundle] URLForResource: self.currentSoundPVC
withExtension: @"caf"];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject);
AudioServicesPlayAlertSound (soundFileObject);
and that actually works! It plays a sound and vibrates the phone simultaneously.
2) phone on, silent mode off, app on, in background, screen unlocked
Now, I set up my uilocalnotification alarms in app delegate's applicationDidEnterBackground, using the following code
NSString *Sound = [self.currentSoundPVC stringByAppendingString:@".caf"];
UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];
alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
[alarm setSoundName:Sound];
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
and this only partially works! The sound gets played but there is NO VIBRATION! Now, this I think is a bug because I am quoting from the apple developer website,
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html, Scheduling the Delivery of Local Notifications section:
"When the notification is sent and the sound is played, the system also triggers a vibration on devices that support it. "
Now, obviously that is NOT THE CASE for my iPhone and obviously the simulator doesn't vibrate, so I can't test this and would like to have this addressed in the developer community!
3) phone on, silent mode off, app on, in foreground, screen locked
Same as #2
4) phone on, silent mode off, app on, in background, screen locked
Same as #2
5) phone on, silent mode off, app off (background process deleted)
Same as #2, because the uilocalnotifications were never cancelled, so the iOS still thinks they are valid.
6) phone on, silent mode on, app on, in background, screen unlocked
no sound, no vibration, NOTHING! This sucks! I would have hoped that apple would have come up with something that works straight out of the box, as usual!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些是选项和效果(均假设音量设置为某个合理值):
选项 1:
配置:
行为:
选项 2:
配置:
行为:
选项 3:
配置:
行为:
选项 4:
配置:
行为:
These are the options and the effects (all assume the volume is set to some reasonable value):
Option 1:
Configuration:
Behaviour:
Option 2:
Configuration:
Behaviour:
Option 3:
Configuration:
Behaviour:
Option 4:
Configuration:
Behaviour:
以下适用于声音和振动:
我知道这可能听起来有点傻,但是您是否在 iPhone 设置中关闭了振动?
the following works with a sound and a vibration:
I know this might sound a bit silly, but have you perhaps turned off vibration in your iPhone settings?
很抱歉将此作为答案发布,但我的评论太长,无法发布在您的评论下。所以我只是在那里放了一个片段,其余的放在这里。这是对tony Million的回答的回复。
嗨托尼,
感谢您的输入,但是我不确定您更改的哪些内容产生了影响。如果我没记错的话,您更改了以下项目:
1)scheduleLocalNotification --> presentLocalNotificationNow
2) 显式设置 UILN RepeatInterval
3) 显式设置 UILN AlertBody
4) 显式设置 UILN AlertAction
除了这四个更改之外,我没有看到其他任何更改。您认为以下哪些因果变化对您产生了预期的影响。
哈哈……没必要觉得这是一个愚蠢的问题。虽然我在帖子中提到了“静音模式开/关”,但我没有具体说明这是什么意思。我特别谈论的是手机侧面的开关。我什至没有想到要检查 iPhone 设置!哈哈!所以,这是一个好问题!然而不幸的是,它并没有被关闭,并且在 OrigPost 的场景 #1 中它已经成功振动。
Sorry for posting this as an answer but my comment was too long to post under yours. So I just put a snippet there and the rest here. This was a reply to tony million's answer.
Hi Tony,
Thanks for your input, however I am not sure which of the things that you changed had an effect. If I am noting correctly you changed the following items:
1) scheduleLocalNotification --> presentLocalNotificationNow
2) explicitly setting UILN repeatInterval
3) explicitly setting UILN alertBody
4) explicitly setting UILN alertAction
Other than those four changes, I don't see anything else. Which of these causal changes would you think had the desired effect for you.
Lol...no need to feel like that was a silly question. Although, I did mention in my post, "silent mode on/off", I did not specify what that meant. I was talking particularly about the switch on the side of the phone. It didn't even occur to me to check the iPhone settings! Haha! So, it was a good question! Unfortunately, however, it wasn't turned off and it had successfully vibrated when in scenario #1 from OrigPost.