ios中設置收到消息推送時,前后台自定義聲音或音樂


主要我們收到訂單推送時,要設置聲音提示

首先一定要調用靜態庫#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 mainBundlepathForResource:@"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); // 需要執 這個 法,選擇 是否提醒 戶,有BadgeSoundAlert三種類型可以選擇設置

}

 

 

2.如果要設置消息收到時顯示系統聲音,那么就設置在收到消息的地方開啟聲音

  //設置聲音,其中1312是系統聲音編號,可以選擇你喜歡的

//     AudioServicesPlaySystemSound(1312);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM