在ios10中添加本地通知 - swift 3

问题描述:

编辑: 因此,将应用程序置于后台的方法确实有效。在ios10中添加本地通知 - swift 3

原文:

所以我一直在努力的通知添加到新UNUserNotificationCenter,但我似乎并没有得到它。

我的视图控制器具有一个动作:

@IBAction func sendPressed(_ sender: AnyObject) { 
    let content = UNMutableNotificationContent() 

    content.title = "Hello" 
    content.body = "What up?" 
    content.sound = UNNotificationSound.default() 

    // Deliver the notification in five seconds. 
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false) 
    let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger) 

    // Schedule the notification. 
    let center = UNUserNotificationCenter.current() 
    center.add(request) { (error) in 
     print(error) 
    } 
    print("should have been added") 
} 

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 

    let center = UNUserNotificationCenter.current() 
    center.requestAuthorization([.alert, .sound]) { (granted, error) in 
    } 
} 

而且我在项目Notification Content Extension为好,但它似乎并没有在所有触发,任何想法是什么我失踪?我正在尝试用户文档中的示例,但它没有告诉我更多,或者我错过了它。

这里:https://developer.apple.com/reference/usernotifications/unmutablenotificationcontent

另外: https://developer.apple.com/reference/usernotificationsui https://developer.apple.com/reference/usernotifications

+0

您可以参考以下链接了解详细使用http://jayprakashdubey.blogspot.in/2016/07/local- notification-in-ios-10-for_31.html –

+0

如何设置不同的日期组件或如何多次调用本地通知? – ArgaPK

您需要为通知注册...我想和这个作品。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     // Override point for customization after application launch. 
     let center = UNUserNotificationCenter.current() 
     center.requestAuthorization([.alert, .sound]) { (granted, error) in 
      // Enable or disable features based on authorization. 
     } 
     return true 
    } 

编辑:你不需要把你的应用程序在后台从iOS的10起PRESENT通知。

使用下面的回调来配置前台通知。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 

Here是一个示例项目。

+0

是的,我有这个,仍然没有运气。你有一个完整的应用程序的例子,所以我可以看到,如果我错过了什么? – Bjarte

+0

请参阅最新的答案... –

+0

唉唉,有它在后台做的伎俩! – Bjarte

用Objective-C的implemation:

我在这里写了一个演示项目:iOS10AdaptationTips

  1. 进口UserNotifications

    ///Notification become independent from Foundation 
    @import UserNotifications; 
    
  2. 为localNotification

    请求授权
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) 
             completionHandler:^(BOOL granted, NSError * _Nullable error) { 
              if (!error) { 
               NSLog(@"request authorization succeeded!"); 
               [self showAlert]; 
              } 
             }]; 
    

    请求授权: enter image description here

  3. 时间表localNotification

  4. 更新应用程序图标徽章数量

    //  //Deliver the notification at 08:30 everyday 
        //  NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; 
        //  dateComponents.hour = 8; 
        //  dateComponents.minute = 30; 
        //  UNCalendarNotificationTrigger *trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:dateComponents repeats:YES]; 
    
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; 
        content.title = [NSString localizedUserNotificationStringForKey:@"Elon said:" arguments:nil]; 
        content.body = [NSString localizedUserNotificationStringForKey:@"Hello Tom!Get up, let's play with Jerry!" 
                     arguments:nil]; 
        content.sound = [UNNotificationSound defaultSound]; 
    
        /// 4. update application icon badge number 
        content.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1); 
        // Deliver the notification in five seconds. 
        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger 
                    triggerWithTimeInterval:5.f repeats:NO]; 
        UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"FiveSecond" 
                          content:content trigger:trigger]; 
        /// 3. schedule localNotification 
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
        [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { 
         if (!error) { 
          NSLog(@"add NotificationRequest succeeded!"); 
         } 
        }]; 
    

那么就会出现这样的:

背景: enter image description here 锁屏:
enter image description here

如果重复用默认只显示一个 enter image description here ,而不是显示在许多的iOS9锁定屏幕上: http://i67.tinypic.com/98t75s.jpg ,同时还支持3D触摸自动 http://a67.tinypic.com/dorw3b.jpg

我这里写了演示:iOS10AdaptationTips

+0

嗨,我跑你演示,但它给了我例外**断言失败 - [UNTimeIntervalNotificationTrigger _initWithTimeInterval:重复:] **执行时** UNTimeIntervalNotificationTrigger *触发= [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5.f重复:是]; ** 。 –

+1

你为什么写Objective-C? – 4thSpace

+9

因为他可以;) – CMash

我解决我的问题如下:(火力地堡,斯威夫特3):

查找AppDelegate中此方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

找到这行:

completionHandler() 

年底成立:如果你不能通过你的演示选项,以completionHandler方法

completionHandler([.alert,.sound,.badge]) 

通知不点火。

+0

没有看到一个巨大的框架的角度做一些简单。 – Bjarte

+0

Apple's或Firebase's? –

+0

这应该真的没有框架使用。这很简单 –

我已经为斯威夫特3的实施可以帮助,您可以点击此处查看:https://*.com/a/45381380/2296630