5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-20 19:09:29 +08:00

can you tell i'm not a go programmer?

This commit is contained in:
popaprozac 2025-02-26 19:57:21 -08:00
parent a807c269cc
commit ad749721e9
3 changed files with 23 additions and 29 deletions

View File

@ -28,9 +28,9 @@ var (
notificationChannelsLock sync.Mutex
nextChannelID int
)
var BundleIdentifierError = fmt.Errorf("notifications require a bundled application with a unique bundle identifier")
const AppleDefaultActionIdentifier = "com.apple.UNNotificationDefaultActionIdentifier"
const BundleIdentifierError = fmt.Errorf("notifications require a bundled application with a unique bundle identifier")
// Creates a new Notifications Service.
// Your app must be packaged and signed for this feature to work.
@ -62,7 +62,7 @@ func CheckBundleIdentifier() bool {
}
// RequestNotificationAuthorization requests permission for notifications.
func (ns *Service) RequestNotificationAuthorization() (bool, err) {
func (ns *Service) RequestNotificationAuthorization() (bool, error) {
if !CheckBundleIdentifier() {
return false, BundleIdentifierError
}
@ -76,7 +76,7 @@ func (ns *Service) RequestNotificationAuthorization() (bool, err) {
}
// CheckNotificationAuthorization checks current notification permission status.
func (ns *Service) CheckNotificationAuthorization() (bool, err) {
func (ns *Service) CheckNotificationAuthorization() (bool, error) {
if !CheckBundleIdentifier() {
return false, BundleIdentifierError
}
@ -241,17 +241,17 @@ func (ns *Service) RemoveNotification(identifier string) error {
//export requestNotificationAuthorizationResponse
func requestNotificationAuthorizationResponse(channelID C.int, authorized C.bool, errorMsg *C.char) {
resultCh, exists := getAndDeleteChannel(int(channelID))
resultCh, exists := getChannel(int(channelID))
if !exists {
// handle this
return
}
var err error
if errorMsg != nil {
err = fmt.Errorf("%s", C.GoString(errorMsg))
C.free(unsafe.Pointer(errorMsg))
}
if errorMsg != nil {
err = fmt.Errorf("%s", C.GoString(errorMsg))
C.free(unsafe.Pointer(errorMsg))
}
resultCh <- notificationChannel{
authorized: bool(authorized),
@ -263,17 +263,17 @@ func requestNotificationAuthorizationResponse(channelID C.int, authorized C.bool
//export checkNotificationAuthorizationResponse
func checkNotificationAuthorizationResponse(channelID C.int, authorized C.bool, errorMsg *C.char) {
resultCh, exists := getAndDeleteChannel(int(channelID))
resultCh, exists := getChannel(int(channelID))
if !exists {
// handle this
return
}
var err error
if errorMsg != nil {
err = fmt.Errorf("%s", C.GoString(errorMsg))
C.free(unsafe.Pointer(errorMsg))
}
if errorMsg != nil {
err = fmt.Errorf("%s", C.GoString(errorMsg))
C.free(unsafe.Pointer(errorMsg))
}
resultCh <- notificationChannel{
authorized: bool(authorized),
@ -300,7 +300,7 @@ func didReceiveNotificationResponse(jsonPayload *C.char) {
ns := NotificationService
notificationServiceLock.RUnlock()
if service != nil {
if ns != nil {
ns.callbackLock.RLock()
callback := ns.notificationResponseCallback
ns.callbackLock.RUnlock()
@ -318,10 +318,7 @@ func registerChannel() (int, chan notificationChannel) {
id := nextChannelID
nextChannelID++
resultCh := make(chan notificationChannel{
authorized bool
err error
}, 1)
resultCh := make(chan notificationChannel, 1)
notificationChannels[id] = resultCh
return id, resultCh

View File

@ -6,8 +6,8 @@
#import <Foundation/Foundation.h>
bool checkBundleIdentifier(void);
bool requestNotificationAuthorization(void *channelPtr);
bool checkNotificationAuthorization(void *channelPtr);
void requestNotificationAuthorization(int channelID);
void checkNotificationAuthorization(int channelID);
void sendNotification(const char *identifier, const char *title, const char *subtitle, const char *body, const char *data_json, void *completion);
void sendNotificationWithActions(const char *identifier, const char *title, const char *subtitle, const char *body, const char *categoryId, const char *actions_json, void *completion);
void registerNotificationCategory(const char *categoryId, const char *actions_json, bool hasReplyField, const char *replyPlaceholder, const char *replyButtonTitle);
@ -17,8 +17,4 @@ void removePendingNotification(const char *identifier);
void removeAllDeliveredNotifications(void);
void removeDeliveredNotification(const char *identifier);
extern void requestNotificationAuthorizationResponse(int channelID, bool authorized, const char* error);
extern void checkNotificationAuthorizationResponse(int channelID, bool authorized, const char* error);
extern void didReceiveNotificationResponse(const char *jsonPayload);
#endif /* NOTIFICATIONS_DARWIN_H */

View File

@ -2,6 +2,10 @@
#import <Cocoa/Cocoa.h>
#import <UserNotifications/UserNotifications.h>
extern void requestNotificationAuthorizationResponse(int channelID, bool authorized, const char* error);
extern void checkNotificationAuthorizationResponse(int channelID, bool authorized, const char* error);
extern void didReceiveNotificationResponse(const char *jsonPayload);
@interface NotificationsDelegate : NSObject <UNUserNotificationCenterDelegate>
@end
@ -80,15 +84,12 @@ bool checkBundleIdentifier(void) {
return true;
}
bool requestNotificationAuthorization(int channelID) {
void requestNotificationAuthorization(int channelID) {
ensureDelegateInitialized();
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionAlert | UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (error) {
requestNotificationAuthorizationResponse(channelID, false, [[error localizedDescription] UTF8String]);
@ -98,7 +99,7 @@ bool requestNotificationAuthorization(int channelID) {
}];
}
bool checkNotificationAuthorization(int channelID) {
void checkNotificationAuthorization(int channelID) {
ensureDelegateInitialized();
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];