【问题标题】:Increase time of custom local notification sound in objective c ios在objective c ios中增加自定义本地通知声音的时间
【发布时间】:2018-03-28 07:11:28
【问题描述】:

我想增加local notification的自定义铃声时间,但是

苹果说:

声音文件的长度必须小于 30 秒。如果声音文件超过 30 秒,则系统播放默认声音。

Here is the documentation

但我的声音文件是 5 分钟

我试过这样:

content.sound = [soundName:@"mycustomtone.aiff"];
content.time = [timeDuration: 300];

我知道 300 秒超过 30 秒,这不是定义时间的正确方法,因为没有通知声音时间的时间定义。

如果可能的话,帮助我将自定义声音的持续时间增加到 30 秒以上!

如果开发者愿意,我知道没有什么是不可能的!

这是我正在做的事情:

 #import "ViewController.h"

 @interface ViewController () {

 NSUserDefaults *defaults;
 }

 @end

 bool isGrantedNotificationAccess;

 @implementation ViewController

 - (void)viewDidLoad {
 [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

isGrantedNotificationAccess = false;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert+UNAuthorizationOptionSound;

[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
    isGrantedNotificationAccess = granted;
}];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
 }


 - (IBAction)notifyButton:(id)sender {


 if (isGrantedNotificationAccess) {
    NSLog(@"clicked");

    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

    content.title = @"Notification Title";
    content.subtitle = @"Notification Subtitle";
    content.body = @"Notification body";
    content.sound = [UNNotificationSound soundNamed:@"alarm_clock_2015.aiff"];
    content.timeDuration = 300;


UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];

UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UYLocalNotification" content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:nil];
}
}
@end

【问题讨论】:

  • 本地通知从哪里调用?你不能让通知的默认声音静音并创建一个不同的函数来播放声音吗?
  • @Egghead 我已经用代码更新了问题,我该如何定义函数你会回答吗?

标签: ios objective-c audio uilocalnotification duration


【解决方案1】:

如你所说:

声音文件的长度必须少于 30 秒。如果声音文件 超过30秒,系统播放默认声音。

我建议您考虑这一点,因为即使您设法欺骗并克服了问题,Apple 也会拒绝您的应用。

除此之外,5 分钟不是声音,而是音乐,所以要在应用程序在后台播放应用程序时,您必须注册为音频应用程序,并在收到通知后使用 AVAudioPlayer 播放文件.

http://www.sagorin.org/ios-playing-audio-in-background-audio/

【讨论】:

    【解决方案2】:

    我会试一试,但我无法测试这段代码,但我会给你一个粗略的解释,我将如何去做这件事..(不知道这是否可行)

    我会创建一个这样的函数:

    -(void)playsound {
        [[NSSound soundNamed:@"alarm_clock_2015"] play];
    }
    

    并从您的 notifyButton IBAction 调用该函数

    - (IBAction)notifyButton:(id)sender {
    
    
     if (isGrantedNotificationAccess) {
        NSLog(@"clicked");
    
        [self playsound];
    
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
    //Mute the sound of this notification
        content.title = @"Notification Title";
        content.subtitle = @"Notification Subtitle";
        content.body = @"Notification body";
        content.sound = [UNNotificationSound soundNamed:@"alarm_clock_2015.aiff"];
        //content.timeDuration = 300;
    
    
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:60 repeats:YES];
    
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"UYLocalNotification" content:content trigger:trigger];
    
    [center addNotificationRequest:request withCompletionHandler:nil];
    }
    }
    

    【讨论】:

    • content.timeDuration中没有timeDuration函数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2017-03-10
    • 1970-01-01
    相关资源
    最近更新 更多