UILocalNotification 不起作用...iPhone 4 iOS 4.3 上没有振动

发布于 2024-10-26 19:09:09 字数 1905 浏览 5 评论 0原文

我会在这里写尽可能多的内容,但标题几乎说明了一切。我在几种不同的情况下测试过它:

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

,但这仅部分有效!声音可以播放,但没有振动!现在,我认为这是一个错误,因为我引用了苹果开发者网站,

http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html,安排本地通知的传送部分:

“当发送通知并播放声音时,系统还会在支持它的设备上触发振动“

现在,显然我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半透明的墙 2024-11-02 19:09:09

这些是选项和效果(均假设音量设置为某个合理值):

选项 1:

配置:

  • [开发人员控制] UILocalNotification 中设置的有效声音名称 = 否
  • [用户控制] 应用程序的通知中心设置:声音 = N/A
  • [用户控制] 静音模式(在 iPhone 侧面开关)= N/A

行为:

  • 播放声音并通知 = 否
  • 振动通知 = 否

选项 2:

配置:

  • 在 UILocalNotification 中设置的有效声音名称 = 是
  • 通知中心设置应用程序:声音 = 关闭
  • 静音模式(在 iPhone 一侧打开)= N/A

行为:

  • 播放声音时有通知 = 否
  • 振动通知 = 否

选项 3:

配置:

  • 在 UILocalNotification 中设置的有效声音名称 = 是
  • 通知中心设置应用程序:声音 = 开启
  • 静音模式(在 iPhone 一侧切换)= 否(未选择)

行为:

  • 播放声音时通知 = 是
  • 通知振动 = 否

选项 4:

配置:

  • 在 UILocalNotification 中设置的有效声音名称 = 是
  • 通知中心设置应用程序:声音 = 开启
  • 静音模式(在 iPhone 侧面打开)= 是(已选择)

行为:

  • 播放声音并通知 = 否
  • 振动通知 = 是

These are the options and the effects (all assume the volume is set to some reasonable value):

Option 1:

Configuration:

  • [Developer controlled] Valid sound name set in UILocalNotification = No
  • [User controlled] Notification Centre setting for the app: Sound = N/A
  • [User controlled] Silent mode (switch on side of iPhone) = N/A

Behaviour:

  • Sound played with notification = No
  • Vibrate with notification = No

Option 2:

Configuration:

  • Valid sound name set in UILocalNotification = Yes
  • Notification Centre setting for the app: Sound = Off
  • Silent mode (switch on side of iPhone) = N/A

Behaviour:

  • Sound played with notification = No
  • Vibrate with notification = No

Option 3:

Configuration:

  • Valid sound name set in UILocalNotification = Yes
  • Notification Centre setting for the app: Sound = On
  • Silent mode (switch on side of iPhone) = No (Not selected)

Behaviour:

  • Sound played with notification = Yes
  • Vibrate with notification = No

Option 4:

Configuration:

  • Valid sound name set in UILocalNotification = Yes
  • Notification Centre setting for the app: Sound = On
  • Silent mode (switch on side of iPhone) = Yes (Selected)

Behaviour:

  • Sound played with notification = No
  • Vibrate with notification = Yes
森林迷了鹿 2024-11-02 19:09:09

以下适用于声音和振动:

        // Create a new notification
        UILocalNotification * notif = [[[UILocalNotification alloc] init] autorelease];
        if (notif)
        {
            notif.repeatInterval    = 0;
            notif.alertBody         = @"NOTIFICATION!!"];
            notif.soundName         = @"sound.caf";             
            notif.alertAction       = NSLocalizedString(@"View", @"View");

            [[UIApplication sharedApplication]  presentLocalNotificationNow:notif];
        }

我知道这可能听起来有点傻,但是您是否在 iPhone 设置中关闭了振动?

the following works with a sound and a vibration:

        // Create a new notification
        UILocalNotification * notif = [[[UILocalNotification alloc] init] autorelease];
        if (notif)
        {
            notif.repeatInterval    = 0;
            notif.alertBody         = @"NOTIFICATION!!"];
            notif.soundName         = @"sound.caf";             
            notif.alertAction       = NSLocalizedString(@"View", @"View");

            [[UIApplication sharedApplication]  presentLocalNotificationNow:notif];
        }

I know this might sound a bit silly, but have you perhaps turned off vibration in your iPhone settings?

千と千尋 2024-11-02 19:09:09

很抱歉将此作为答案发布,但我的评论太长,无法发布在您的评论下。所以我只是在那里放了一个片段,其余的放在这里。这是对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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文