iOS推送注册相关知识点
兼容iOS7 iOS8的注册方式
- (void)registRemoteNotification
{
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0)
{
//iOS8 以后注册方式
[[UIApplication sharedApplication] registerForRemoteNotifications];
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
else
{
//iOS8以前注册方式
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
}
}
判断用户是否注册推送
+ (BOOL)isResigterNotification {
if (iOS8以上) {
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (UIUserNotificationTypeNone != setting.types) {
return YES;
}
} else {
UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if(UIRemoteNotificationTypeNone != type){
return YES;
}
return NO;
}
option 相关
UIUserNotificationTypeNone = 0 , == 0000 0
UIUserNotificationTypeBadge = 1 << 0 , == 0001 1左移0位 2^0 = 1
UIUserNotificationTypeSound = 1 << 1 , == 0010 1左移1位 2^1 = 2
UIUserNotificationTypeAlert = 1 << 2 , == 0100 1左移2位 2^2 = 4
假如用户勾选推送时显示badge和提示sound,那么types的值就是 == 0001 | 0010 = 0011 == 2^0 + 2 ^1 = 3
消息提示
-
对于简单的、无混音音频,AVAudio ToolBox框架提供了一个简单的C语言风格的音频服务
- 使用AudioservicesPlaySystemSound函数来播放简单的声音。要遵守以下几个规则:
1.音频长度小于30秒
2.格式只能是PCM或者IMA4
3.文件必须被存储为.caf、.aif、或者.wav格式
4.简单音频不能从内存播放,而只能是磁盘文件
除了音频限制外,对播放也没有控制权。音量位当前设备的音量。使用此函数无法循环播放,无法控制音效。但是可以设置播放结束回调函数,在音频播放结束后,做一些操作。
- 使用AudioservicesPlaySystemSound函数来播放简单的声音。要遵守以下几个规则:
-
震动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); -
系统提示音
AudioServicesPlaySystemSound(1007); -
自定义提示音
/音效文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"wav"];
//组装并播放音效
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
//添加音频结束时的回调 (SoundFinished回调函数)
AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, SoundFinished,sample);
AudioServicesPlaySystemSound(soundID);
//声音停止
AudioServicesDisposeSystemSoundID(soundID); -
播放结束回调函数
static void SoundFinished(SystemSoundID soundID,void* sample){
/*播放全部结束,因此释放所有资源 */
AudioServicesDisposeSystemSoundID(sample);
CFRelease(sample);
CFRunLoopStop(CFRunLoopGetCurrent());
}