-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Conversation
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 |
4c4ee06
to
2eca350
Compare
For some reason @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 👍 |
@collinjackson / @kroikie / @franciscojma86 can I ask you to review this? |
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.///
).flutter analyze
) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?