app_darwin.m 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //go:build !ci
  2. // +build !ci
  3. #import <Foundation/Foundation.h>
  4. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
  5. #import <UserNotifications/UserNotifications.h>
  6. #endif
  7. static int notifyNum = 0;
  8. extern void fallbackSend(char *cTitle, char *cBody);
  9. bool isBundled() {
  10. return [[NSBundle mainBundle] bundleIdentifier] != nil;
  11. }
  12. #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
  13. void doSendNotification(UNUserNotificationCenter *center, NSString *title, NSString *body) {
  14. UNMutableNotificationContent *content = [UNMutableNotificationContent new];
  15. [content autorelease];
  16. content.title = title;
  17. content.body = body;
  18. notifyNum++;
  19. NSString *identifier = [NSString stringWithFormat:@"fyne-notify-%d", notifyNum];
  20. UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
  21. content:content trigger:nil];
  22. [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
  23. if (error != nil) {
  24. NSLog(@"Could not send notification: %@", error);
  25. }
  26. }];
  27. }
  28. void sendNotification(char *cTitle, char *cBody) {
  29. UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  30. NSString *title = [NSString stringWithUTF8String:cTitle];
  31. NSString *body = [NSString stringWithUTF8String:cBody];
  32. UNAuthorizationOptions options = UNAuthorizationOptionAlert;
  33. [center requestAuthorizationWithOptions:options
  34. completionHandler:^(BOOL granted, NSError *_Nullable error) {
  35. if (!granted) {
  36. if (error != NULL) {
  37. NSLog(@"Error asking for permission to send notifications %@", error);
  38. // this happens if our app was not signed, so do it the old way
  39. fallbackSend((char *)[title UTF8String], (char *)[body UTF8String]);
  40. } else {
  41. NSLog(@"Unable to get permission to send notifications");
  42. }
  43. } else {
  44. doSendNotification(center, title, body);
  45. }
  46. }];
  47. }
  48. #else
  49. void sendNotification(char *cTitle, char *cBody) {
  50. fallbackSend(cTitle, cBody);
  51. }
  52. #endif