Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[firebase_messaging] Add macOS support #1989

Closed
wants to merge 9 commits into from

Conversation

cbenhagen
Copy link
Contributor

@cbenhagen cbenhagen commented Feb 12, 2020

Description

Add macOS support.

Related Issues

#1653

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • If the pull request affects only one plugin, the PR title starts with the name of the plugin in brackets (e.g. [cloud_firestore])
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy.
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

@cbenhagen cbenhagen changed the title [firebase_messaging] Add macOS support [firebase_messaging] [WIP] Add macOS support Feb 14, 2020
@cbenhagen
Copy link
Contributor Author

cbenhagen commented Feb 16, 2020

Diff for convenience as git will not show it due to the symlink:

--- FLTFirebaseMessagingPlugin-old.m	2020-06-27 19:20:25.000000000 +0200
+++ FLTFirebaseMessagingPlugin.m	2020-06-27 19:30:54.000000000 +0200
@@ -1,6 +1,7 @@
 // Copyright 2017 The Chromium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
+#include <TargetConditionals.h>
 
 #import <UserNotifications/UserNotifications.h>
 
@@ -10,7 +11,7 @@
 
 NSString *const kGCMMessageIDKey = @"gcm.message_id";
 
-#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
+#if TARGET_OS_OSX || (defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0)
 @interface FLTFirebaseMessagingPlugin () <FIRMessagingDelegate>
 @end
 #endif
@@ -37,7 +38,10 @@
                                   binaryMessenger:[registrar messenger]];
   FLTFirebaseMessagingPlugin *instance =
       [[FLTFirebaseMessagingPlugin alloc] initWithChannel:channel];
+// TODO(cbenhagen): Enable for macOS when https://github.com/flutter/flutter/issues/41471 is done.
+#if TARGET_OS_IPHONE
   [registrar addApplicationDelegate:instance];
+#endif
   [registrar addMethodCallDelegate:instance channel:channel];
 
   SEL sel = NSSelectorFromString(@"registerLibrary:withVersion:");
@@ -66,7 +70,7 @@
   NSString *method = call.method;
   if ([@"requestNotificationPermissions" isEqualToString:method]) {
     NSDictionary *arguments = call.arguments;
-    if (@available(iOS 10.0, *)) {
+    if (@available(macOS 10.14, iOS 10.0, *)) {
       UNAuthorizationOptions authOptions = 0;
       NSNumber *provisional = arguments[@"provisional"];
       if ([arguments[@"sound"] boolValue]) {
@@ -80,7 +84,7 @@
       }
 
       NSNumber *isAtLeastVersion12;
-      if (@available(iOS 12, *)) {
+      if (@available(macOS 10.14, iOS 12, *)) {
         isAtLeastVersion12 = [NSNumber numberWithBool:YES];
         if ([provisional boolValue]) authOptions |= UNAuthorizationOptionProvisional;
       } else {
@@ -116,9 +120,13 @@
                               }];
                           result([NSNumber numberWithBool:granted]);
                         }];
-
+#if TARGET_OS_IPHONE
       [[UIApplication sharedApplication] registerForRemoteNotifications];
+#else
+      [[NSApplication sharedApplication] registerForRemoteNotifications];
+#endif
     } else {
+#if TARGET_OS_IPHONE
       UIUserNotificationType notificationTypes = 0;
       if ([arguments[@"sound"] boolValue]) {
         notificationTypes |= UIUserNotificationTypeSound;
@@ -136,10 +144,29 @@
 
       [[UIApplication sharedApplication] registerForRemoteNotifications];
       result([NSNumber numberWithBool:YES]);
+#else
+      NSRemoteNotificationType notificationTypes = 0;
+      if ([arguments[@"sound"] boolValue]) {
+        notificationTypes |= NSRemoteNotificationTypeSound;
+      }
+      if ([arguments[@"alert"] boolValue]) {
+        notificationTypes |= NSRemoteNotificationTypeAlert;
+      }
+      if ([arguments[@"badge"] boolValue]) {
+        notificationTypes |= NSRemoteNotificationTypeBadge;
+      }
+
+      [[NSApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
+      result([NSNumber numberWithBool:YES]);
+#endif
     }
   } else if ([@"configure" isEqualToString:method]) {
     [FIRMessaging messaging].shouldEstablishDirectChannel = true;
+#if TARGET_OS_IPHONE
     [[UIApplication sharedApplication] registerForRemoteNotifications];
+#else
+    [[NSApplication sharedApplication] unregisterForRemoteNotifications];
+#endif
     if (_launchNotification != nil && _launchNotification[kGCMMessageIDKey]) {
       [_channel invokeMethod:@"onLaunch" arguments:_launchNotification];
     }
@@ -173,7 +200,11 @@
         NSLog(@"deleteInstanceID, error: %@", error);
         result([NSNumber numberWithBool:NO]);
       } else {
+#if TARGET_OS_IPHONE
         [[UIApplication sharedApplication] unregisterForRemoteNotifications];
+#else
+        [[NSApplication sharedApplication] unregisterForRemoteNotifications];
+#endif
         result([NSNumber numberWithBool:YES]);
       }
     }];
@@ -189,7 +220,7 @@
   }
 }
 
-#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
+#if TARGET_OS_OSX || (defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0)
 // Received data message on iOS 10 devices while app is in the foreground.
 // Only invoked if method swizzling is enabled.
 - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
@@ -235,6 +266,7 @@
 
 #pragma mark - AppDelegate
 
+#if TARGET_OS_IPHONE
 - (BOOL)application:(UIApplication *)application
     didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   if (launchOptions != nil) {
@@ -308,4 +340,36 @@
   [_channel invokeMethod:@"onMessage" arguments:remoteMessage.appData];
 }
 
+#else
+- (void)applicationDidFinishLaunching:(NSNotification *)notification {
+  if (notification != nil) {
+    _launchNotification = notification.userInfo[NSApplicationLaunchUserNotificationKey];
+  }
+}
+
+- (void)applicationDidEnterBackground:(NSApplication *)application {
+  _resumingFromBackground = YES;
+}
+
+- (void)applicationDidBecomeActive:(NSApplication *)application {
+  _resumingFromBackground = NO;
+}
+
+- (BOOL)application:(NSApplication *)application
+    didReceiveRemoteNotification:(NSDictionary *)userInfo {
+  [self didReceiveRemoteNotification:userInfo];
+  return YES;
+}
+
+- (void)messaging:(nonnull FIRMessaging *)messaging
+    didReceiveRegistrationToken:(nonnull NSString *)fcmToken {
+  [_channel invokeMethod:@"onToken" arguments:fcmToken];
+}
+
+- (void)messaging:(FIRMessaging *)messaging
+    didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage {
+  [_channel invokeMethod:@"onMessage" arguments:remoteMessage.appData];
+}
+#endif
+
 @end

@cbenhagen cbenhagen changed the title [firebase_messaging] [WIP] Add macOS support [firebase_messaging] Add macOS support Feb 19, 2020
@cbenhagen
Copy link
Contributor Author

For some reason UserAgent.h sometimes is searched in the darwin directory. @AndroidNils is having the same issue on #1705.

@franciscojma86 || @collinjackson do you have an idea why? I am sure I must be forgetting something stupid.

@nilsreichardt
Copy link
Contributor

For some reason UserAgent.h sometimes is searched in the darwin directory. @AndroidNils is having the same issue on #1705.

@franciscojma86 || @collinjackson do you have an idea why? I am sure I must be forgetting something stupid.

Any updates?

@nilsreichardt
Copy link
Contributor

For some reason UserAgent.h sometimes is searched in the darwin directory. @AndroidNils is having the same issue on #1705.
@franciscojma86 || @collinjackson do you have an idea why? I am sure I must be forgetting something stupid.

Any updates?

@cbenhagen There is a fix for this issue as a PR: #2099 👍

@cbenhagen
Copy link
Contributor Author

@collinjackson / @kroikie / @franciscojma86 can I ask you to review this?

@Salakar Salakar closed this in #4012 Nov 3, 2020
@cbenhagen cbenhagen deleted the firebase_messaging_macos branch November 4, 2020 16:13
@firebase firebase locked and limited conversation to collaborators Dec 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants