阿木申 申楠

热衷编程技术 | 申楠 : qq:38371354 | msn:amushen2005@hotmail.com

导航

iOS收到Push后播放声音和震动

Posted on 2014-03-25 10:28  阿木申  阅读(9454)  评论(0编辑  收藏  举报

一、APNS

1.注册


[cpp]
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert]; 

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert];
2.服务器推送(JAVA)


[java]
PushNotificationPayload payLoad =  PushNotificationPayload.fromJSON(message);  
              
        payLoad.addAlert("iphone推送测试 www.baidu.com"); // 消息内容   
              
        payLoad.addBadge(count); // iphone应用图标上小红圈上的数值   
              
        payLoad.addSound("default"); // 铃音 默认  

PushNotificationPayload payLoad =  PushNotificationPayload.fromJSON(message);
            
        payLoad.addAlert("iphone推送测试 www.baidu.com"); // 消息内容
            
        payLoad.addBadge(count); // iphone应用图标上小红圈上的数值
            
        payLoad.addSound("default"); // 铃音 默认
二、程序内

1.震动

添加系统框架:


[cpp]
#import <AudioToolbox/AudioToolbox.h> 

#import <AudioToolbox/AudioToolbox.h>
调用震动代码:


[cpp]
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
2.消息声音

2.1 系统声音


[cpp]
AudioServicesPlaySystemSound(1007); 

AudioServicesPlaySystemSound(1007);其中1007是系统声音的编号,其他的可用编号:

iphone系统声效

 

2.2 用户音效


[cpp]
//音效文件路径  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"wav"]; 
    //组装并播放音效  
    SystemSoundID soundID; 
    NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; 
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID); 
    AudioServicesPlaySystemSound(soundID); 
        //声音停止  
        AudioServicesDisposeSystemSoundID(soundID); 

//音效文件路径
 NSString *path = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"wav"];
 //组装并播放音效
 SystemSoundID soundID;
 NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
 AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
 AudioServicesPlaySystemSound(soundID);
        //声音停止
        AudioServicesDisposeSystemSoundID(soundID);