简体   繁体   English

使iPhone在本地通知下震动

[英]Make iPhone vibrate on Local Notification

I am currently developing an App with VoIP functionality. 我目前正在开发具有VoIP功能的应用程序。 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). 但是,我想让手机在整个自定义声音(30秒)内振动。 Is there any "build-in" way? 有什么“内置”方式吗? Or would I have to actively trigger vibration from my AppDelegate when I receive my VoIP Push? 还是在收到VoIP Push时必须主动触发AppDelegate的振动? Can I face any AppStore certification problems if I do it from there? 如果从那里开始,我是否会遇到任何AppStore认证问题? (Facebook Messenger and WeChat provide this behaviour for example, so there has to be a way :) ) (例如,Facebook Messenger和微信提供了此行为,因此必须有一种方法:))

Thanks in advance Rafael 在此先感谢拉斐尔

There are two seemingly similar functions that take a parameter kSystemSoundID_Vibrate: 有两个看似相似的函数带有参数kSystemSoundID_Vibrate:

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

Both of the functions vibrate the iPhone. 这两个功能都使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. 因此,如果要连续振动设备,如常识所述,请使用功能2作为警报。首先,在构建阶段中将AudioToolbox框架AudioToolbox.framework添加到目标中。

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. 另外,如果您想每5秒振动一次(或其他任何时间),则可以执行此操作。

ViewController.h ViewController.h

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

ViewController.m 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] 您可以通过调用[timer invalidate]来停止计时器(当用户接听电话时)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM