Rafael Regh February 2016

Make iPhone vibrate on Local Notification

I am currently developing an App with VoIP functionality. When a new call comes in I am sending out a notification with a custom sound. If the phone is on vibrate, it vibrates once by default. However I would like to make the phone vibrate during the entire, custom sound (30seconds). Is there any "build-in" way? Or would I have to actively trigger vibration from my AppDelegate when I receive my VoIP Push? Can I face any AppStore certification problems if I do it from there? (Facebook Messenger and WeChat provide this behaviour for example, so there has to be a way :) )

Thanks in advance Rafael

Answers


NeilAppleDeveloper February 2016

There are two seemingly similar functions that take a parameter kSystemSoundID_Vibrate:

1) AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
2) AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Both of the functions vibrate the iPhone. But, when you use the first function on devices that don’t support vibration, it plays a beep sound. The second function, on the other hand, does nothing on unsupported devices. So if you are going to vibrate the device continuously, as an alert, common sense says, use function 2. First, add the AudioToolbox framework AudioToolbox.framework to your target in Build Phases.

Then, import this header file:

#import <AudioToolbox/AudioServices.h>

Also, if you want to vibrate once every 5 second (or whatever), you can do this.

ViewController.h

NSTimer *timer //declare this globally (in header file)

ViewController.m

timer = [NSTimer scheduledTimerWithTimeInterval:YOURINTERVAL target:self selector:@selector(YOURSELECTOR) userInfo:nil repeats:YES];

-(void)YOURSELECTOR{
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

You can stop timer (when user answer the phone) by calling [timer invalidate]

Post Status

Asked in February 2016
Viewed 2,771 times
Voted 4
Answered 1 times

Search




Leave an answer