Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UILocalNotification custom sound is not playing in iOS7

I'm using UILocalNotification in an application.

In the application there are two sounds which are played conditionally- I have applied proper conditions for them.

But when I install the application and run it on an iOS 7 device, then it fires the local notification but a sound is not playing in the application.

Code is given below to set the notification:

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    if (localNotification == nil)
        return;
    localNotification.fireDate = [pickerView date];

    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    if (alarm_number == 1) {
        localNotification.alertBody = [NSString stringWithFormat:@"First Alarm"];
    }
    else
    {
        localNotification.alertBody = [NSString stringWithFormat:@"Second Alarm"];
    }

    localNotification.alertAction =@"Ok";

    NSString *message = [[NSString alloc]initWithString:@""];

    if(alarm_number == 1)
    {
        localNotification.soundName=@"Alarm_1.mp3";
        message = @"First Alarm Scheduled";
    }
    else
    {
        localNotification.soundName=@"Alarm_2.mp3";
        message = @"Second Alarm Scheduled";
    }

    localNotification.applicationIconBadgeNumber = 1;

    // Specify custom data for the notification
    NSString *alarmString;
    if (alarm_number==1) {
        alarmString = [NSString stringWithFormat:@"First Alarm"];
    }
    else
    {
        alarmString = [NSString stringWithFormat:@"Second Alarm"];
    }

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:alarmString forKey:@"AlarmFor"];

    localNotification.userInfo = infoDict;

    // Schedule the notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [localNotification release];

What I have checked for is the Sound setting in the Settings/Notification Centre app for my app.

Please go through 1st to 3rd image to see what I have checked.

(1) Notification Center

(1) Notification Center

(2) Application

enter image description here

(3) Sound is off here

enter image description here

So, to enable this I have checked Inter App Audio at Capabilities in Targets of the application and it was Off as shown in the image below.

Capabilities in Inter-app audio

Capabilities in Inter-app audio

Then I have changed it to On and it looks like shown in the image below.

Capabilities after changed On

Yet, it still does not play any sound in iOS 7 devices.

Does anybody have any idea about why is it not working? It would be a great help.

Thanks.

like image 723
Jayeshkumar Sojitra Avatar asked Oct 12 '13 05:10

Jayeshkumar Sojitra


3 Answers

Did you try to turn off "Do not disturb" mode off? I have the same problem, then I found out "Do not Disturb" is on. After turn off that, it all works the right way.

like image 105
wzhang84 Avatar answered Oct 17 '22 02:10

wzhang84


try using .caf file instead of .mp3

afconvert -f caff -d LEI16@44100 -c 1 wolf.wav wolf_out.caf
like image 22
thesummersign Avatar answered Oct 17 '22 01:10

thesummersign


My guess is your sound isn't formatted appropriately to play by the OS. Make sure your sound is formatted in IMA4 format. Also, make sure your file is in your application bundle, and you should be good to go.

For more reference and similar question, see this SO question.

Choose custom sound for Local Notifications

Here is a link for Apple's Local and Push Notification Guide. It explains the sounds in detail and the default sounds that can be used.

like image 28
vijay Avatar answered Oct 17 '22 02:10

vijay