【问题标题】:Local Notification in WatchOS 3WatchOS 3 中的本地通知
【发布时间】:2016-12-20 14:44:31
【问题描述】:

我正在使用 WatchOS 3 测试版并尝试在手表上启动本地通知。该界面只是一个按钮,它在下面的代码中调用“buttonPushed”方法。该应用程序运行良好,但我从未收到通知。应用程序结构是 Xcode 8 中 WatchKit 应用程序的默认结构。

此代码在 WatchKit 扩展的 InterfaceController.swift 文件中

我是否遗漏了一些非常明显的东西?

@IBAction func buttonPushed() {
        sendMyNotification()
    }

    func sendMyNotification(){
        if #available(watchOSApplicationExtension 3.0, *) {

            let center = UNUserNotificationCenter.current()

            center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
                // Enable or disable features based on authorization.
            }


            let content = UNMutableNotificationContent()
            content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)
            content.body = NSString.localizedUserNotificationString(forKey: "Hello_message_body", arguments: nil)
            content.sound = UNNotificationSound.default()
            content.categoryIdentifier = "REMINDER_CATEGORY"
            // 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.

            center.add(request ,withCompletionHandler: nil)



        } else {
            // Fallback on earlier versions
        }


    }

【问题讨论】:

    标签: ios notifications watchos-3


    【解决方案1】:

    根据this。您应该为每次请求指定一个唯一的新标识符。

    我的:

    let id = String(Date().timeIntervalSinceReferenceDate)
    let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)
    

    【讨论】:

    • 您也可以使用let id = UUID.init().uuidString创建一个唯一标识的字符串
    • 一生一次可能是两个相同的 UUID。
    【解决方案2】:

    Swift 4 简单代码

        let content = UNMutableNotificationContent()
        content.title = "How many days are there in one year"
        content.subtitle = "Do you know?"
        content.body = "Do you really know?"
        content.badge = 1
    
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    

    【讨论】:

      【解决方案3】:

      本地通知 watchOS swift 4.0

      var content = UNMutableNotificationContent()
      content.title = "ALERT !"
      content.body = msg
      content.sound = UNNotificationSound.default() as? UNNotificationSound
      // Time
      var trigger: UNTimeIntervalNotificationTrigger?
      trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
      // Actions
      var snoozeAction = UNNotificationAction(identifier: "Track", title: "Track", options: .foreground)
      
      var category = UNNotificationCategory(identifier: "UYLReminderCategory", actions: [snoozeAction], intentIdentifiers: [] as? [String] ?? [String](), options: .customDismissAction)
      var categories = Set<AnyHashable>([category])
      
      center.setNotificationCategories(categories as? Set<UNNotificationCategory> ?? Set<UNNotificationCategory>())
      
      content.categoryIdentifier = "UYLReminderCategory"
      
      var identifier: String = stringUUID()
      
      var request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
      
      center.add(request, withCompletionHandler: {(_ error: Error?) -> Void in
      if error != nil {
          print("Something went wrong: \(error)")
      }
      })
      

      唯一的请求标识符方法

      func stringUUID() -> String {
           let uuid = UUID()
           let str: String = uuid.uuidString
           return str
      }
      

      目标 C

       // Objective-C
         UNMutableNotificationContent *content = [UNMutableNotificationContent new];
         content.title = @"ALERT !";
         content.body = msg;
         content.sound = [UNNotificationSound defaultSound];
      
      // Time
      
         UNTimeIntervalNotificationTrigger *trigger;
      
         trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1
                                                                                                      repeats:NO];
      // Actions
         UNNotificationAction *snoozeAction = [UNNotificationAction actionWithIdentifier:@"Track"
                                                                                title:@"Track" options:UNNotificationActionOptionForeground];
      
      // Objective-C
        UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"UYLReminderCategory"
                                                                                actions:@[snoozeAction] intentIdentifiers:@[]
                                                                                options:UNNotificationCategoryOptionCustomDismissAction];
         NSSet *categories = [NSSet setWithObject:category];
      
      // Objective-C
         [center setNotificationCategories:categories];
      
      // Objective-C
          content.categoryIdentifier = @"UYLReminderCategory";
      
          NSString *identifier = [self stringUUID];
          UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                            content:content trigger:trigger];
      
          [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
          if (error != nil) {
              NSLog(@"Something went wrong: %@",error);
          }
          }];
      

      唯一的请求标识符方法

      -(NSString *)stringUUID {
         NSUUID *uuid = [NSUUID UUID];
         NSString *str = [uuid UUIDString];
         return str;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        • 2019-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多