当需要进行推出通知的时候写入如下代码
BOOL isAppActivity = [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive;
if (isAppActivity) {
//此处为当前APP在前台,做些自己此时想干的事
return;
}
UILocalNotification *noti = [[UILocalNotification alloc] init];
noti.fireDate = [NSDate date];
noti.alertBody = message?message:@"UpDate!";
//使用系统默认的推送通知提醒
noti.soundName = UILocalNotificationDefaultSoundName;
noti.applicationIconBadgeNumber += 1;//角标
[[UIApplication sharedApplication] scheduleLocalNotification:noti];
当用户点击推出的通知进入app的时候跑如下方法:
//收到本地推送,并点击进入app
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//在这里,就是用户通过你推出的通知点击进来的地方
//就可以为所欲为了,比如
//重置角标、直达该通知产生的页面、、、
}
播放系统振动+声音和循环振动+响铃
直接撸代码:
static PlaySystemSound *_instance;
@implementation PlaySystemSound
+ (PlaySystemSound *)Instance{
if (!_instance) {
_instance = [[PlaySystemSound alloc]init];
}
return _instance;
}
//参数可以不传,播放默认
//若是播放自定义声音,不可以超过30s长度
- (id)initSystemSoundWithName:(NSString *)soundName SoundType:(NSString *)soundType
{
self = [PlaySystemSound Instance];
if (self) {
NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",soundName?soundName:@"shake",soundType?soundType:@"caf"];
if (path) {
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&cafSound);
if (error != kAudioServicesNoError) {//获取的声音的时候,出现错误
cafSound = -1;
}
}
}
return self;
}
//最简单的响一下,不需要管理资源,不需要自己进行停止
- (void)shortPlay{
AudioServicesPlaySystemSound(cafSound);
}
//调用系统的循环播放,需要自己停止
- (void)play
{
//systemAudioCallback,是一个C类型的函数,每次播放结束去掉用
AudioServicesAddSystemSoundCompletion(cafSound, NULL, NULL, systemAudioCallback, NULL);
// AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSound(cafSound);
}
//我在此处直接延迟1.2s停止播放了
//也就是无论播放了多久以及播放到哪,都会直接remove
void systemAudioCallback(){
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[_instance stop];
});
}
- (void)stop{
// AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
AudioServicesRemoveSystemSoundCompletion(cafSound);
}
**下面的方法是在调用系统私有API,当上架AppStore的时候会找事的。所以,无论好用与否,我只当做是涨涨姿势了。**
//void AudioServicesStopSystemSound(int);
//void AudioServicesPlaySystemSoundWithVibration(int, id, NSDictionary *);
//
//- (void)playSoundWithPriviteAPI{
// NSMutableDictionary* dict = [NSMutableDictionary dictionary];
// NSMutableArray* arr = [NSMutableArray array];
//
//
//
// [arr addObject:[NSNumber numberWithBool:YES]]; //vibrate for 2000ms
// [arr addObject:[NSNumber numberWithInt:2000]];
//
//
//
// [dict setObject:arr forKey:@"VibePattern"];
// [dict setObject:[NSNumber numberWithFloat:0.3] forKey:@"Intensity"];
//
//
//
// AudioServicesStopSystemSound(kSystemSoundID_Vibrate);
// NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
// NSArray *pattern = @[@YES, @30, @NO, @1];
//
//
//
// dictionary[@"VibePattern"] = pattern;
// dictionary[@"Intensity"] = @1;
//
//
// AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, dictionary);
//}
当然啦,这些只是一个菜鸟的实现方式,欢迎进行评论、提出好的实现方式方法。