iOS - 仅声音推送通知

iOS - Push Notification with Sound only

当我的应用程序未激活或关闭时收到推送通知时,我的设备会播放通知声音。当我的应用程序处于活动或打开状态时,它不会在收到通知/消息时播放声音。

我希望即使在应用程序处于活动/打开状态时也能播放通知声音。请有人可以帮助我实现它吗?

  -(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];

}

我的通知是这样的:

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

你必须在你的 HandleNotification 方法中播放声音,只需在里面添加。

AudioServicesPlaySystemSound(1004);

试试这个:

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);

您无法访问通知默认声音,因此您应该在某处下载并添加到您的项目中