关闭应用程序(不在后台)时如何处理新的iOS10通知操作?
当应用最小化时,一切都可以正常使用:
UNUserNotificationCenter.current().delegate = x
Run Code Online (Sandbox Code Playgroud)
并处理它
class x: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {
}
}
Run Code Online (Sandbox Code Playgroud)
但是在应用程序关闭并且用户在通知中轻按操作时什么也没叫...也许我无法处理后台任务,而且我总是必须启动应用程序?
我已经集成了APNS,并希望在远程通知中显示图像,如下所示;
我已经在下面的代码中使用了参考链接;
AppDelegate.h
#import <UserNotifications/UserNotifications.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate>
Run Code Online (Sandbox Code Playgroud)
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self registerForRemoteNotification];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"mainscreen"];
self.window.rootViewController = vc1;
return YES;
}
- (void)registerForRemoteNotification
{
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}];
}
else {
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication …Run Code Online (Sandbox Code Playgroud) objective-c apple-push-notifications ios ios10 usernotifications
在WWDC会话之一中,我获得了用于更新现有通知的代码段。我认为这行不通。尝试更新通知内容。
首先,我请求UNUserNotificationCenter始终有效的待处理通知。然后,我创建新请求以使用现有的唯一标识符更新通知。
有1个新变量content: String。
// Got at least one pending notification.
let triggerCopy = request!.trigger as! UNTimeIntervalNotificationTrigger
let interval = triggerCopy.timeInterval
let newTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval, repeats: true)
// Update notificaion conent.
let notificationContent = UNMutableNotificationContent()
notificationContent.title = NSString.localizedUserNotificationString(forKey: "Existing Title", arguments: nil)
notificationContent.body = content
let updateRequest = UNNotificationRequest(identifier: request!.identifier, content: notificationContent, trigger: newTrigger)
UNUserNotificationCenter.current().add(updateRequest, withCompletionHandler: { (error) in
if error != nil {
print(" Couldn't update notification \(error!.localizedDescription)")
}
})
Run Code Online (Sandbox Code Playgroud)
我无法捕获错误。问题在于通知内容主体 不会更改。
如何使用新的(来自iOS 10)通知框架UserNotifications https://developer.apple.com/reference/usernotifications?language=objc,始终(即使在前台)显示本地通知?
我正在设置多个UNUsernotnotifications,如下所示,
- (void)viewDidLoad {
[super viewDidLoad];
notifCount = 0;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"request succeeded!");
[self set10Notif];
}
}];
}
Run Code Online (Sandbox Code Playgroud)
在该set10Notif方法中,我设置多个(8个用于测试)通知,时间与当前时间相差10秒.
-(void) set10Notif
{
notifCount = notifCount+1;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0") && notifCount < 10)
{
// create actions
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
[calendar setTimeZone:[NSTimeZone localTimeZone]];
NSDate *fireD = [[NSDate date] dateByAddingTimeInterval:notifCount*10];
NSString *fireStr = [self returnStringFromDate:fireD withFormat:@"hh/mm/ss dd/MM/yyyy"];
NSDateComponents *components …Run Code Online (Sandbox Code Playgroud) objective-c ios ios10 usernotifications unusernotificationcenter
我对swift很新,我正在尝试调用多个函数来请求UISwitch IBAction中的本地通知.我想在某个日期发送通知 - 一年中的每个季度在第4,7,10,1个月.只有第四季度的功能才被调用.如何调用所有四个函数?这是我的代码:
// UISwitch用于季度通知
@IBAction func quarterlyFrequencyTapped(_ sender: UISwitch) {
if quarterlyFrequency.isOn == true {
firstQuarter(); secondQuarter(); thirdQuarter(); fourthQuarter()
print("quarterly frequency is \(quarterlyFrequency.isOn)")
} else {
removeQuarterlyNotification()
print("quaterly frequency is \(monthlyFrequency.isOn)")
}
}
Run Code Online (Sandbox Code Playgroud)
//所有四个季度的功能
func firstQuarter() {
let firstQuarterContent = UNMutableNotificationContent()
firstQuarterContent.title = "First Quarter"
firstQuarterContent.subtitle = "Some string"
firstQuarterContent.body = "Some other string"
var firstQuarterDate = DateComponents()
firstQuarterDate.month = 3
firstQuarterDate.day = 11
firstQuarterDate.hour = 19
firstQuarterDate.minute = 20
let firstQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: firstQuarterDate, repeats: true)
let …Run Code Online (Sandbox Code Playgroud) 因此,以下代码用于从图像的本地存储URL附加图像。我检查Terminal一下是否存储了图像,并且确实存储了图像,没有任何问题。因此,排除url本身的任何问题。
do {
let attachment = try UNNotificationAttachment(identifier: imageTag, url: url, options: nil)
content.attachments = [attachment]
} catch {
print("The attachment was not loaded.")
}
Run Code Online (Sandbox Code Playgroud)
创建UserNotification时附带的其他代码可以正常工作,因为它会在正确的指定时间触发。
代码总是转到catch块。如果实现中有任何人可以请我指出错误。请帮忙。谢谢。
编辑:print(error.localizedDescription)错误消息为Invalid attachment file URL。
Edit2:print(error)错误消息为Error Domain=UNErrorDomain Code=100 "Invalid attachment file URL" UserInfo={NSLocalizedDescription=Invalid attachment file URL}
我目前正在将应用程序迁移到新的UserNotifications框架.由于用户打开了本地通知,我一直在检测应用是否已启动.测试用例是:
问题是在这种情况下userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:不会被调用.在application:didFinishLaunchingWithOptions:一个键UIApplicationLaunchOptionsLocalNotificationKey中包含一个类型的对象,UIConcreteLocalNotification它是一个子类UILocalNotification.但是,作为旧通知系统的一部分,UILocalNotification已弃用,我们不应该使用它.我在文档和网络中挖掘并没有找到我的问题的答案:
如何通过本地通知找到应用程序是否已启动?
我如何获得该通知?
notifications ios ios10 usernotifications unusernotificationcenter
我正在尝试学习Swift,并正在阅读有关推送通知的教程.
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: nil)
Run Code Online (Sandbox Code Playgroud)
给我错误
"没有更多的上下文,表达的类型是模糊的".
我直接从教程中复制/粘贴了这一行,并在StackOverFlow上找到了相同的行.
我究竟做错了什么?
我正在使用Xcode 8.
在iOS 9之前的所有内容中,任何时候都可以安排64个通知限制.新的通知系统是否仍然如此,或者我可以提前安排新的UNUserNotifications吗?