watchOS自定义通知

2021-07-27  本文已影响0人  聆听随风

自定义iWatch 标题栏颜色,通知字体颜色,实现如下。

1.发送通知

        let center = UNUserNotificationCenter.current()// 获取当前通知中心
        center.delegate = self
        center.requestAuthorization(options: [.alert, .sound, .badge]) { (isSuccess, error) in// 设定通知提示
        print("regist is \(isSuccess && error == nil ? "success" : "failure")")// 判断是否配置成功
        }
        center.delegate = self// 设定代理.注意代理要遵守UNUserNotificationCenterDelegate
        
        let content = UNMutableNotificationContent()
        content.title = NSLocalizedString("notification_title", comment: "")
        //content.subtitle = "子标题1111"
        content.categoryIdentifier = "myCateGory" // WatchKit App interface.storyboard 中需要设置一致
        content.body = NSLocalizedString("notification_not_connect_content", comment: "")
        content.badge = 0
        content.sound = UNNotificationSound.default

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
        let request = UNNotificationRequest(identifier: "Notification", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request) { err in
            err != nil ? print("添加本地通知错误0", err!.localizedDescription) : print("添加本地通知成功0")
        }

2. 建立Notification Interface Controller

新建后完后,一定要修改Notofication Category 名字,这个名字和发送通知地方名字一定要保持一致!!!
否则设定的标题烂颜色和标题烂背景颜色不起作用。


设定样式图.png
上一篇下一篇

猜你喜欢

热点阅读