Mina Fawzy
Mina Fawzy

Reputation: 21452

iOS - Push Notification with Sound only

When I get a push notification while my application is not active or closed, my device plays a notification sound. When my application is active or open, it does not play a sound on receiving a notification / message.

I want the notification sound to play even when the application is active / open. Please can someone help me achieve it?

  -(void)HandleNotification:(NSDictionary *)userInfo{

    NSMutableDictionary * test = [userInfo objectForKey:@"aps"];
    NSMutableDictionary *alert_msg = [test objectForKey:@"alert"];
    NoteType = [alert_msg objectForKey:@"type"];
    Note_Student_id = [alert_msg objectForKey:@"sid"];

    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"School Link"
                        message:[alert_msg objectForKey:@"title"]
                                                    delegate:self
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
    [alert show];

}

My notification looks like this:

  {
            aps =     {
                alert =         {
                    sid = 114;
                    title = fddsf;
                    type = 2;
                };
                sound = default;
            };
        }

Upvotes: 0

Views: 1158

Answers (2)

gabuh
gabuh

Reputation: 1166

You will have to play the sound in your HandleNotification method, just add inside.

AudioServicesPlaySystemSound(1004);

Upvotes: 1

3CC
3CC

Reputation: 502

Try this:

NSString* soundName = [[userInfo objectForKey: @"aps"] objectForKey:@"sound"];

if([soundName isEqual: @"default"])
    soundName = @"default_sound_name.mp3";

NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@""];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);

you can not access notification default sound, so you should download it somewhere and add it to your project

Upvotes: 1

Related Questions