admin管理员组

文章数量:1390822

I'm trying to push notifies by python with objc on mac os(12.7.6).But i'm not familiar with objc , mac Notification api. i have written follow code

from UserNotifications import UNUserNotificationCenter, UNTimeIntervalNotificationTrigger, UNMutableNotificationContent, UNNotificationRequest, UNNotificationAction, UNNotificationActionOptions, UNNotificationCategory, UNNotificationCategoryOptions


center = UNUserNotificationCenter.currentNotificationCenter()
trigger = UNTimeIntervalNotificationTrigger(triggerWithTimeInterval=1, repeats=False)

content = UNMutableNotificationContent()
content.setTitle_('Main Title')
content.setSubTitle_('sub Title')
content.setBody_('message content')

content.setUserInfo_(["method", "new"])


acceptAction = UNNotificationAction(identifier="SHOW_ACTION", title="Show", options=UNNotificationActionOptions.UNNotificationActionOptionForeground)
declineAction = UNNotificationAction(identifier="declineAction", title="Close", options=UNNotificationActionOptions.UNNotificationActionOptionForeground)

testCategory = UNNotificationCategory(identifier="NOTIFICATION_DEMO", actions=[acceptAction, declineAction], intentIdentifiers=[], hiddenPreviewsBodyPlaceholder="", options=UNNotificationCategoryOptions.UNNotificationCategoryOptionCustomDismissAction)

request = UNNotificationRequest(identifier='NOTIFICATION_DEMO_REQUEST', content=content, trigger=trigger)

center.setNotificationCategories([testCategory])

center.add(request)

I want push a notify, then the notify window will display messages and two buttons, one is "Show", another is "Close". When pressed "Show" button , i want to do some action. Please help.

本文标签: macospython push a notify on macStack Overflow