主要我們收到訂單推送時,要設置聲音提示
首先一定要調用靜態庫#import <AudioToolbox/AudioToolbox.h>
設置代碼如下,在收到訂單推送的地方判斷
// iOS 10 Support,程序在前台時
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger)
)completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]
]) {
//判斷是新訂單還是退貨
NSString *str =[NSString stringWithFormat:@"%@",userInfo[@"aps"][@"alert"]];
NSLog(@"user-----%@",userInfo);
if ([str containsString:@"新訂單"]) {
//音效文件路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"haoyebao" ofType:@"wav"];
這里是指你的音樂名字和文件類型
NSLog(@"path---%@",path);
//組裝並播放音效
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
AudioServicesPlaySystemSound(soundID);
}else if([str containsString:@"退貨"]){
}else{
}
[JPUSHService handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要執 這個 法,選擇 是否提醒 戶,有Badge、Sound、Alert三種類型可以選擇設置
}
2.如果要設置消息收到時顯示系統聲音,那么就設置在收到消息的地方開啟聲音
//設置聲音,其中1312是系統聲音編號,可以選擇你喜歡的
// AudioServicesPlaySystemSound(1312);