记录下自己是怎么实现本地推送(LocalNotification)的,以及播放声音、振动的实现
2017-01-12 17:45
1251 查看
当需要进行推出通知的时候写入如下代码
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); //}
当然啦,这些只是一个菜鸟的实现方式,欢迎进行评论、提出好的实现方式方法。
4000
相关文章推荐
- iOS 实现后台 播放音乐声音 AVAudioPlayer 以及铃声设置(循环播放震动)
- 推送本地项目至Github遇到的问题以及解决办法记录
- iOS之推送通知-本地-服务器3.服务器端实现: 如果要编写内容提供者的推送服务程序,需要进行SSL认证编程,以及构建APNS数据包,数据包分为3个主要部分:Command(命令)、deviceTo
- 使用switchButton组件实现阻止和允许百度云推送以及控制推送声音,整了好久!
- 自己的服务器怎么关联极光的SDK实现推送?
- Android使用MediaPlayer播放流媒体,支持远程以及本地流媒体,一行代码实现
- iOS 本地推送以及自定义推送声音
- Android webview实现h5视频全屏播放兼容Android7.0,自己添加webview库兼容全部版本
- iOS 中的本地推送实现
- Cocos2dx - -播放声音和音效的实现
- 闹钟功能实现+本地通知+音频播放
- 环信接收消息本地推送的实现
- 看别人怎么学技术,并记录自己的发展
- 使用Vitamio打造自己的Android万能播放器(4)——本地播放(快捷搜索、数据存储)
- Android ExoPlayer实现网络URL、本地视频播放
- 使用自己实现的Renderer Filter+GDI解码和播放视频
- unity关于屏幕震动以及物体振动的一个实现方法
- 自己写的一个ArrayLIST,but 不知道怎么很好的实现System底下的arraycopy方法
- 长尾关键词记录,怎么快速实现长尾词排名
- 简单代码在ABAP中实现声音的播放