Can I send a Local Notification with no sound?

2019-02-23 09:47发布

I want to have a few local notifications with no display and no sound, but only a vibration. I can get it to do no display, by simply setting alertBody to @"", but how can I send no sound? I am thinking if I don't get a better way from you guys, that I will be able to just make a sound that is empty, add it to my project, then set soundName to that sound. But is there any sort of default way to do this?

If I add a phony sound name it still plays the default notification sound.

Thanks!!

3条回答
叼着烟拽天下
2楼-- · 2019-02-23 10:01

No you can not disable sound because UILocalNotification does not provide any option for this. So better option is as you told in your question to use a empty sound file.

查看更多
聊天终结者
3楼-- · 2019-02-23 10:12

Yes you can add another sound file.

NSString *soundFile=@"temp.mp3"; 

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    if (localNotification==nil) {
        return;
    }
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
    localNotification.alertBody = @"Your alert message";
    localNotification.soundName = soundFile;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

In above code just put the name of sound file which you have saved in your resources in place of "soundFile" string.

查看更多
Melony?
4楼-- · 2019-02-23 10:14

You can not set sound.or

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)responsewithCompletionHandler:(void (^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNCalendarNotificationTrigger class]]) {}
else{}
//don't write UNNotificationPresentationOptionSound
completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);

}

查看更多
登录 后发表回答