我已经使用以下iOS 8,9代码:
UIMutableUserNotificationAction *action1;
action1 = [[UIMutableUserNotificationAction alloc] init];
[action1 setActivationMode:UIUserNotificationActivationModeBackground];
[action1 setTitle:@"REJECT"];
[action1 setIdentifier:NotificationActionOneIdent];
[action1 setDestructive:NO];
[action1 setAuthenticationRequired:NO];
UIMutableUserNotificationAction *action2;
action2 = [[UIMutableUserNotificationAction alloc] init];
[action2 setActivationMode:UIUserNotificationActivationModeBackground];////UIUserNotificationActivationModeBackground
[action2 setTitle:@"ACCEPT"];
[action2 setIdentifier:NotificationActionTwoIdent];
[action2 setDestructive:NO];
[action2 setAuthenticationRequired:NO];
UIMutableUserNotificationCategory *actionCategory;
actionCategory = [[UIMutableUserNotificationCategory alloc] init];
[actionCategory setIdentifier:NotificationCategoryIdent];
[actionCategory setActions:@[action1, action2]
forContext:UIUserNotificationActionContextDefault];
NSSet *categories = [NSSet setWithObject:actionCategory];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
Run Code Online (Sandbox Code Playgroud)
委托方法处理:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
//Handle according to app state …Run Code Online (Sandbox Code Playgroud) iphone objective-c apple-push-notifications ios usernotificationsui
所以我可以安排这样的通知;
//iOS 10 Notification
if #available(iOS 10.0, *) {
var displayDate: String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = DateFormatter.Style.full
return dateFormatter.string(from: datePicker.date as Date)
}
let notif = UNMutableNotificationContent()
notif.title = "I am a Reminder"
notif.subtitle = "\(displayDate)"
notif.body = "Here's the body of the notification"
notif.sound = UNNotificationSound.default()
notif.categoryIdentifier = "reminderNotification"
let today = NSDate()
let interval = datePicker.date.timeIntervalSince(today as Date)
let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: false)
let request = UNNotificationRequest(identifier: "reminderNotif", content: notif, trigger: notifTrigger)
UNUserNotificationCenter.current().add(request, …Run Code Online (Sandbox Code Playgroud) 通知扩展的宽高比可以通过UNNotificationExtensionInitialContentSizeRatio扩展Info.plist文件中的属性来定义。但是可以在各自UIViewController加载后以某种方式更改此值吗?
Apple网站上的文档表明确实有可能:
扩展加载后,您可以更改视图控制器的大小。
但是如何?我已经尝试过更改扩展程序的main UIView框架,就像这样...
func didReceive(_ notification: UNNotification) {
self.view.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: 100.0)
self.view.setNeedsUpdateConstraints()
self.view.setNeedsLayout()
}
Run Code Online (Sandbox Code Playgroud)
...但是只有视图的框架已正确更新,整个通知容器的底部都留有空白区域。
我还尝试了一些不太明显的解决方案,包括在控制器生命周期的不同点更改框架并UIWindow直接设置框架。到目前为止,都没有成功。
知道是否可以通过编程方式更改高度吗?
预先感谢您的任何答复。
我正在将 UIImage 保存到 Core Data 中。所以首先,我将其转换为 NSData,然后保存。
我需要在保存图像后获取该图像的 URL。我这样做是因为我想安排一个带有附件的本地通知,据我所知,唯一的方法就是使用 URL。
这是我的代码:
//my image:
var myImage: UIImage?
var imageData: NSData?
if let image = myImage {
imageData = UIImageJPEGRepresentation(image, 0.5)! as NSData
}
myEntity.setValue(imageData, forKey: "image")
Run Code Online (Sandbox Code Playgroud)
这就是我应该向通知添加附件的方式:
UNNotificationAttachment.init(identifier: String, url: URL>, options: [AnyHashable : Any]?)
当用户点击按钮保存图像时,我正在保存图像并手动安排通知。
如果您需要额外信息,请告诉我。
core-data ios swift unnotificationattachment usernotificationsui