2014-12-11 16 views
7

Tôi đang làm việc trên ứng dụng x nhanh chóng và tôi đang gặp sự cố khi hiểu cấu trúc userNoticiation/didActivateNotification. Tôi đã xem lại tài liệu và tìm kiếm SO nhưng vẫn bị kẹt. Mọi hướng dẫn về mã sau sẽ được đánh giá cao:Swift NSUserNotificationCenter didActivateNotification không hoạt động

func notify(message: String, callBack: String) { 


    println(message) 
    println(callBack) 
    var notification:NSUserNotification = NSUserNotification() 
    notification.title = "New Phone Call" 
    notification.informativeText = message 
    notification.actionButtonTitle = "Lookup" 
    notification.hasActionButton = true 
    var center:NSUserNotificationCenter = NSUserNotificationCenter.defaultUserNotificationCenter() 


    center.delegate = self 
    center.scheduleNotification(notification) 



} 
func notify (center: NSUserNotificationCenter, didActivateNotification notification: NSUserNotification){ 
    center.delegate = self 
    println("clicked") //this does not print 
} 

Thông báo hiển thị chính xác như tôi muốn. Nhấp vào nút "tra cứu" mà tôi đã xác định sẽ đưa ứng dụng của tôi lên nền trước khi lần đầu tiên nó được nhấp, nhưng mã tôi mong đợi để xử lý hành động đó không kích hoạt.

Xin cảm ơn trước.

Trả lời

5

Thay đổi tuyên bố 'thông báo func' thứ hai thành 'tùy chọn func userNotificationCenter'

+0

Điều này có thực sự hoạt động không? Tôi đã thử khi nhấp chuột thông báo của tôi không kích hoạt và nó sẽ gửi lỗi "'tùy chọn' chỉ có thể áp dụng cho bộ nhớ giao thức" –

+0

@ChrisR Có thể đó là với Swift 1.x? Bạn đã thử loại bỏ bộ định danh 'tùy chọn' chưa? –

+0

Tài liệu cho hàm ủy quyền 'didActivateNotification' là [tại đây] (https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSUserNotificationCenterDelegate_Protocol/index.html#//apple_ref/occ/intfm/NSUserNotificationCenterDelegate/userNotificationCenter: didActivateNotification :). –

Các vấn đề liên quan