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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/szwq01/article/details/51338226

1.appdelegate 中注册

一、APNS

1.注册


 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

    {

        UIUserNotificationType myTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

        

        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes categories:nil];

        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    }

    else

    {

        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;

        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];

    }

二、程序内

1.震动

添加系统框架:


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

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

AudioServicesPlaySystemSound(1007);//1007 叮咚 1003 嗖嗖

//音效文件路径  
    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);





展开阅读全文

没有更多推荐了,返回首页