我UIUserNotificationType.none在ViewController.swift上使用了Swift3,我得到了这个错误:'none'不可用user []来构造一个空的选项集; 这是我的代码:
func updateUI() {
let currentSettings = UIApplication.shared.currentUserNotificationSettings
if currentSettings?.types != nil {
if currentSettings!.types == [UIUserNotificationType.badge, UIUserNotificationType.alert] {
textField.isHidden = false
button.isHidden = false
datePicker.isHidden = false
}
else if currentSettings!.types == UIUserNotificationType.badge {
textField.isHidden = true
}
else if currentSettings!.types == UIUserNotificationType.none {
textField.isHidden = true
button.isHidden = true
datePicker.isHidden = true
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用UNUserNotificationCenterios 10.对于测试,我从当前时间开始设置本地通知10秒.
这是我试过的,
- (void)viewDidLoad {
[super viewDidLoad];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request succeeded!");
[self set10Notifs];
}
}];
}
-(void) set10Notifs
{
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
#if XCODE_VERSION_GREATER_THAN_OR_EQUAL_TO_8
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSCalendarUnitTimeZone fromDate:[[NSDate date] dateByAddingTimeInterval:10]];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:@"Prayer!" arguments:nil];
objNotificationContent.body = [NSString localizedUserNotificationStringForKey:@"Time now"
arguments:nil]; …Run Code Online (Sandbox Code Playgroud) objective-c ios ios10 usernotifications unusernotificationcenter
我正在更新我的本地通知以使用iOS 10,我遇到了一个问题,我认为nextTrigger函数返回NOT"触发条件将满足的下一个日期."而是返回当前日期时间的任何内容.加上您最初设置UNTimeInvervalNotificationTrigger的内容.
因此,如果您将触发器设置为在60秒内关闭,那么从文档中我期望当我调用nextTriggerDate()时,我会让它返回设置触发器的日期时间+ 60秒.因此,如果我将其设置为12:00:00,我希望nextTriggerDate()将是12:01:00.然而,我所经历的是,无论当前日期是+ 60秒,它都会返回.
我编写了一个调度UNTimeIntervalNotificationTrigger的示例,然后每秒打印出nextTriggerDate().当我运行它时,我每秒都会获得一个新的nextTriggerDate.像这样:
//Optional(2016-11-03 21:26:31 +0000)
//Optional(2016-11-03 21:26:32 +0000)
//Optional(2016-11-03 21:26:33 +0000)
//Optional(2016-11-03 21:26:34 +0000)
//Optional(2016-11-03 21:26:35 +0000)
//Optional(2016-11-03 21:26:36 +0000)
Run Code Online (Sandbox Code Playgroud)
我用苹果打开了TSI,但是......你知道......这需要一段时间.所以我想我会看到这里是否有人有任何见解.我怀疑这是一个错误,如果我得到更多信息,我会更新这个.
这是我用来说明问题的代码:
import UIKit
import UserNotifications
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var setButton: UIButton!
@IBOutlet weak var timePicker: UIPickerView!
weak var timer: Timer?
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func setButtonAction(_ sender: UIButton) {
var mySecond = pickerSelection
// build notification
let content …Run Code Online (Sandbox Code Playgroud) 我正在安排UserNorification每天在特定时间触发,通知用户做某事。但是如果用户在通知被触发前 X 小时这样做,我需要取消今天的通知,在我看来取消所有,然后再次重新安排但从明天的特定时间开始。例如,如果今天通知应该在 11:00 触发,而用户在 10:00“做那件事”,则不应触发 11:00 通知,我需要在同一时间再次安排但从明天开始. 循环不断,明天也一样。我的问题是:
我应该使用以下代码取消安排第一个每日通知:UNUserNotificationCenter.current().removeAllPendingNotificationRequests()?
如何安排从特定日期开始的每日通知?
我正在开发一个应用程序,我需要每天向用户发送大量通知.大约5-40取决于用户.
我正在使用本地通知发送它,但我知道有64个通知限制.这是指每天64个通知,还是总共?
来自https://developer.apple.com/documentation/usernotifications/registering_your_app_with_apns上的 Apple Doc 示例
UIApplication.shared.registerForRemoteNotifications()应用程序启动时始终被调用:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UIApplication.shared.registerForRemoteNotifications()
return true
}
Run Code Online (Sandbox Code Playgroud)
正常吗?或者我们应该使用布尔首选项并检查它是否未注册并调用它?
另外,我需要请求用户授权以在适当的位置显示通知(例如,在我的设置视图控制器中,我有一个用于启用/禁用通知的开关)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
Run Code Online (Sandbox Code Playgroud)
AppDelegate在这种情况下,我似乎必须将所有代码从https://github.com/firebase/quickstart-ios/blob/master/messaging/MessagingExampleSwift/AppDelegate.swift移至我的设置视图控制器,甚至registerForRemoteNotifications()。
因此,与通知(注册、接收)相关的所有委托都将位于“设置视图控制器”中。它会起作用吗?当App未运行时,我会在那个地方收到通知吗?
或者我应该将代码的不同部分放在哪里?
更新
还有为什么他们不使用
UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { settings in
if settings.authorizationStatus == .notDetermined {
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions) { …Run Code Online (Sandbox Code Playgroud) 创建 XCode 项目时,我选择了 Apple Watch Application 并启用了“包含通知”。这导致我的项目中有一个NotificationView.swift和一个NotificationController.swift。
我已经用我想要在本地通知中显示的视图内容填充了NotificationView.swift。
在我的常规 HostingController.swift 中,我现在想使用 NotificationView.swift 的内容安排本地通知。
到目前为止,我正在使用当前的代码:
let userNotificationCenter = UNUserNotificationCenter.current()
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "title"
notificationContent.body = "body"
notificationContent.categoryIdentifier = "categoryNameDummy"
let category = UNNotificationCategory(identifier: "categoryNameDummy", actions: [], intentIdentifiers: [] as? [String] ?? [String](), options: .customDismissAction)
let categories = Set<AnyHashable>([category])
userNotificationCenter.setNotificationCategories(categories as? Set<UNNotificationCategory> ?? Set<UNNotificationCategory>())
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 2, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: notificationContent, trigger: trigger)
userNotificationCenter.add(request) { (error) in
if let error = error …Run Code Online (Sandbox Code Playgroud) 我正在制作一个闹钟应用程序,我已经确认我的闹钟通知被正确触发,但声音并不总是像我预期的那样播放。
例如,当手机未处于静音模式时,通知会成功播放声音(好吧,太棒了!)。然而,当手机是在静默模式下,声音不玩了,即使该应用程序在后台运行还是(没那么大......)。
我知道静音模式应该使所有通知声音静音,但我已经从 App Store 下载了其他闹钟应用程序(如 Alarmy),即使手机处于静音状态,它们也能以某种方式播放通知声音模式,只要应用程序仍在后台运行。只有当应用程序完全退出时,静音模式才会生效。
有谁知道如何实现这个结果?是否需要在代码或 plist 文件中声明某些设置或选项?我已经在互联网上搜索过,但没有找到有关此特定问题的任何信息...
我设置 AVAudioSession 类别的代码:
private func setAudioCategory() {
do {
// Enable sound (even while in silent mode) as long as app is in foreground.
try AVAudioSession.sharedInstance().setCategory(.playback)
}
catch {
print(error.localizedDescription)
}
}
Run Code Online (Sandbox Code Playgroud)
我设置通知的代码:
/// Sets a local user notification for the provided `Alarm` object.
static func set(_ alarm: Alarm) {
// Configure the notification's content.
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: K.Keys.notificationTitle, arguments: …Run Code Online (Sandbox Code Playgroud) nsnotificationcenter ios avaudiosession swift usernotifications
我正在努力audio/video call并尝试将来电呼叫notification循环播放1分钟,就像WhatsApp在iOS应用程序是背景时显示,Notification banner隐藏并显示铃声1分钟.
我试过这段代码,它只触发一次:
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
content.title = [NSString stringWithFormat:@"Video Call from %@",userId];
content.body = @"";
content.userInfo = [userInfo mutableCopy];
content.sound = [UNNotificationSound soundNamed:@""];
NSDate *now = [NSDate date];
now = [now dateByAddingTimeInterval:3];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitTimeZone fromDate:now];
UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:NO];
UNNotificationRequest * request = [UNNotificationRequest requestWithIdentifier:@"INCOMING_VOIP_APN" content:content trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate …Run Code Online (Sandbox Code Playgroud) 我仅在尝试使用 UserNotifications 框架注册远程通知时收到此错误。使用 PushKit 时一切正常。
dispatch_queue_t mainQueue = dispatch_get_main_queue();
// Create a push registry object
self.voipRegistry = [[PKPushRegistry alloc] initWithQueue: mainQueue];
// Set the registry's delegate to self
self.voipRegistry.delegate = self;
// Set the push type to VoIP
self.voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
Run Code Online (Sandbox Code Playgroud)
从 Xcode 11 和 iOS13 开始,PushKit 发生了变化以支持 CallKit,因此我尝试使用 UserNotifications 代替,如Apple 文档中所述
Important
If you are unable to support CallKit in your app, you cannot use PushKit to handle push notifications. Instead, configure your app's push notification support …Run Code Online (Sandbox Code Playgroud) objective-c apple-push-notifications ios pushkit usernotifications