Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/firebase_messaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
## 6.0.0

* Use `UNUserNotificationCenter` to receive messages on iOS version >= 10.
* **Breaking Change** For iOS versions >= 10, this will cause any other plugin that specifies a
`UNUserNotificationCenterDelegate` to `[UNUserNotificationCenter currentNotificationCenter]` to
stop receiving notifications. To have this plugin work with plugins that specify their own
`UNUserNotificationCenterDelegate`, you can remove the line
```objectivec
[UNUserNotificationCenter currentNotificationCenter].delegate = // plugin specified delegate
```

and add this line to your iOS project `AppDelegate.m`

```objectivec
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}
```

## 5.1.9

* Fix strict compilation errors.
Expand Down
65 changes: 65 additions & 0 deletions packages/firebase_messaging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ To integrate your plugin into the iOS part of your app, follow these steps:

1. Follow the steps in the "[Upload your APNs certificate](https://firebase.google.com/docs/cloud-messaging/ios/client#upload_your_apns_certificate)" section of the Firebase docs.

1. Add the following lines to the `(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions`
method in the `AppDelegate.m`/`AppDelegate.swift` of your iOS project.

Objective-C:
```objectivec
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}
```

Copy link

@reecepm reecepm Nov 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, if you are using Swift over Objective C, add the following lines to the

application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool

method in the AppDelegate.swift of your iOS project.

    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out! I added a swift example.

Swift:
```swift
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
```

### Dart/Flutter Integration

From your Dart code, you need to import the plugin and instantiate it:
Expand Down Expand Up @@ -195,6 +212,54 @@ curl https://fcm.googleapis.com/fcm/send -H "Content-Type:application/json" -X P

Remove the `notification` property in `DATA` to send a data message.

You could also test this from within Flutter using the [http](https://pub.dev/packages/http) package:

```dart
// Replace with server token from firebase console settings.
final String serverToken = '<Server-Token>';
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();

Future<Map<String, dynamic>> sendAndRetrieveMessage() async {
await firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true),
);

await http.post(
'https://fcm.googleapis.com/fcm/send',
headers: <String, String>{
'Content-Type': 'application/json',
'Authorization': 'key=$serverToken',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{
'body': 'this is a body',
'title': 'this is a title'
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': await firebaseMessaging.getToken(),
},
),
);

final Completer<Map<String, dynamic>> completer =
Completer<Map<String, dynamic>>();

firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
completer.complete(message);
},
);

return completer.future;
}
```

## Issues and feedback

Please file Flutterfire specific issues, bugs, or feature requests in our [issue tracker](https://github.com/FirebaseExtended/flutterfire/issues/new).
Expand Down
1 change: 0 additions & 1 deletion packages/firebase_messaging/android/gradle.properties

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableJetifier=true
android.useAndroidX=true
android.enableR8=true
4 changes: 4 additions & 0 deletions packages/firebase_messaging/example/ios/Runner/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ @implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate =
(id<UNUserNotificationCenterDelegate>)self;
}
[GeneratedPluginRegistrant registerWithRegistry:self];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';

import 'package:flutter_driver/driver_extension.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
Expand Down
57 changes: 44 additions & 13 deletions packages/firebase_messaging/ios/Classes/FirebaseMessagingPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ @interface FLTFirebaseMessagingPlugin () <FIRMessagingDelegate>
details:error.localizedDescription];
}

static NSObject<FlutterPluginRegistrar> *_registrar;

@implementation FLTFirebaseMessagingPlugin {
FlutterMethodChannel *_channel;
NSDictionary *_launchNotification;
BOOL _resumingFromBackground;
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
_registrar = registrar;
FlutterMethodChannel *channel =
[FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/firebase_messaging"
binaryMessenger:[registrar messenger]];
Expand Down Expand Up @@ -59,22 +62,50 @@ - (instancetype)initWithChannel:(FlutterMethodChannel *)channel {
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
NSString *method = call.method;
if ([@"requestNotificationPermissions" isEqualToString:method]) {
UIUserNotificationType notificationTypes = 0;
NSDictionary *arguments = call.arguments;
if ([arguments[@"sound"] boolValue]) {
notificationTypes |= UIUserNotificationTypeSound;
}
if ([arguments[@"alert"] boolValue]) {
notificationTypes |= UIUserNotificationTypeAlert;
}
if ([arguments[@"badge"] boolValue]) {
notificationTypes |= UIUserNotificationTypeBadge;
if (@available(iOS 10.0, *)) {
UNAuthorizationOptions authOptions = 0;
if ([arguments[@"sound"] boolValue]) {
authOptions |= UNAuthorizationOptionSound;
}
if ([arguments[@"alert"] boolValue]) {
authOptions |= UNAuthorizationOptionAlert;
}
if ([arguments[@"badge"] boolValue]) {
authOptions |= UNAuthorizationOptionBadge;
}

[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError *_Nullable error) {
if (error) {
result(getFlutterError(error));
} else {
result([NSNumber numberWithBool:granted]);
}
}];
} else {
UIUserNotificationType notificationTypes = 0;
if ([arguments[@"sound"] boolValue]) {
notificationTypes |= UIUserNotificationTypeSound;
}
if ([arguments[@"alert"] boolValue]) {
notificationTypes |= UIUserNotificationTypeAlert;
}
if ([arguments[@"badge"] boolValue]) {
notificationTypes |= UIUserNotificationTypeBadge;
}

UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
}
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

result(nil);
[[UIApplication sharedApplication] registerForRemoteNotifications];

if (!@available(iOS 10.0, *)) {
result([NSNumber numberWithBool:YES]);
}
} else if ([@"configure" isEqualToString:method]) {
[FIRMessaging messaging].shouldEstablishDirectChannel = true;
[[UIApplication sharedApplication] registerForRemoteNotifications];
Expand Down
15 changes: 9 additions & 6 deletions packages/firebase_messaging/lib/firebase_messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,17 @@ class FirebaseMessaging {
/// On iOS, prompts the user for notification permissions the first time
/// it is called.
///
/// Does nothing on Android.
void requestNotificationPermissions(
[IosNotificationSettings iosSettings = const IosNotificationSettings()]) {
/// Does nothing and returns null on Android.
FutureOr<bool> requestNotificationPermissions([
IosNotificationSettings iosSettings = const IosNotificationSettings(),
]) {
if (!_platform.isIOS) {
return;
return null;
}
_channel.invokeMethod<void>(
'requestNotificationPermissions', iosSettings.toMap());
return _channel.invokeMethod<bool>(
'requestNotificationPermissions',
iosSettings.toMap(),
);
}

final StreamController<IosNotificationSettings> _iosSettingsStreamController =
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_messaging/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for Firebase Cloud Messaging, a cross-platform
messaging solution that lets you reliably deliver messages on Android and iOS.
author: Flutter Team <[email protected]>
homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging
version: 5.1.9
version: 6.0.0

flutter:
plugin:
Expand Down