使iPhone在本地通知下震动 [英] Make iPhone vibrate on Local Notification

查看:104
本文介绍了使iPhone在本地通知下震动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发具有VoIP功能的应用程序.当有新电话打进来时,我会发出带有自定义声音的通知. 如果手机处于震动状态,则默认情况下会震动一次.但是,我想让手机在整个自定义声音(30秒)内振动. 有什么内置"方式吗?还是在收到VoIP Push时必须主动触发AppDelegate的振动?如果从那里开始,我是否会遇到任何AppStore认证问题? (例如,Facebook Messenger和WeChat提供了这种行为,因此必须有一种方法:))

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 :) )

先谢谢了 拉斐尔

推荐答案

有两个看似相似的函数,其参数为kSystemSoundID_Vibrate:

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

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

这两个功能都会使iPhone震动.但是,当您在不支持振动的设备上使用第一个功能时,它会发出蜂鸣声.另一方面,第二个功能在不受支持的设备上不执行任何操作.因此,如果您要连续振动设备,如常识所说,作为警报,请使用功能2. 首先,在构建阶段中将AudioToolbox框架AudioToolbox.framework添加到目标中.

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.

然后,导入此头文件:

#import <AudioToolbox/AudioServices.h>

此外,如果您想每5秒振动一次(或其他任何时间),则可以执行此操作.

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

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);
}

您可以通过拨打[timer invalidate]

这篇关于使iPhone在本地通知下震动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆