UILocalNotification Not Working … No Vibration on iPhone 4 iOS 4.3

醉酒当歌 提交于 2019-12-23 15:00:13

问题


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!


回答1:


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



回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/5443848/uilocalnotification-not-working-no-vibration-on-iphone-4-ios-4-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!