electron_api_push_notifications_mac.mm 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (c) 2022 Asana, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/api/electron_api_push_notifications.h"
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #import "shell/browser/mac/electron_application.h"
  9. #include "shell/common/gin_converters/value_converter.h"
  10. #include "shell/common/gin_helper/promise.h"
  11. namespace electron::api {
  12. v8::Local<v8::Promise> PushNotifications::RegisterForAPNSNotifications(
  13. v8::Isolate* isolate) {
  14. gin_helper::Promise<std::string> promise(isolate);
  15. v8::Local<v8::Promise> handle = promise.GetHandle();
  16. [[AtomApplication sharedApplication]
  17. registerForRemoteNotificationTypes:NSRemoteNotificationTypeBadge |
  18. NSRemoteNotificationTypeAlert |
  19. NSRemoteNotificationTypeSound];
  20. PushNotifications::apns_promise_set_.emplace_back(std::move(promise));
  21. return handle;
  22. }
  23. void PushNotifications::ResolveAPNSPromiseSetWithToken(
  24. const std::string& token_string) {
  25. std::vector<gin_helper::Promise<std::string>> promises =
  26. std::move(PushNotifications::apns_promise_set_);
  27. for (auto& promise : promises) {
  28. promise.Resolve(token_string);
  29. }
  30. }
  31. void PushNotifications::RejectAPNSPromiseSetWithError(
  32. const std::string& error_message) {
  33. std::vector<gin_helper::Promise<std::string>> promises =
  34. std::move(PushNotifications::apns_promise_set_);
  35. for (auto& promise : promises) {
  36. promise.RejectWithErrorMessage(error_message);
  37. }
  38. }
  39. void PushNotifications::UnregisterForAPNSNotifications() {
  40. [[AtomApplication sharedApplication] unregisterForRemoteNotifications];
  41. }
  42. void PushNotifications::OnDidReceiveAPNSNotification(
  43. const base::Value::Dict& user_info) {
  44. Emit("received-apns-notification", user_info);
  45. }
  46. } // namespace electron::api