Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
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
5 changes: 5 additions & 0 deletions packages/firebase_messaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 5.1.0

* Changed the return type of `subscribeToTopic` and `unsubscribeFromTopic` to
`Future<void>`.

## 5.0.6

* Additional integration tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,38 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
result.success(null);
} else if ("subscribeToTopic".equals(call.method)) {
String topic = call.arguments();
FirebaseMessaging.getInstance().subscribeToTopic(topic);
result.success(null);
FirebaseMessaging.getInstance()
.subscribeToTopic(topic)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Exception e = task.getException();
Log.w(TAG, "subscribeToTopic error", e);
result.error("subscribeToTopic", e.getMessage(), null);
return;
}
result.success(null);
}
});
} else if ("unsubscribeFromTopic".equals(call.method)) {
String topic = call.arguments();
FirebaseMessaging.getInstance().unsubscribeFromTopic(topic);
result.success(null);
FirebaseMessaging.getInstance()
.unsubscribeFromTopic(topic)
.addOnCompleteListener(
new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (!task.isSuccessful()) {
Exception e = task.getException();
Log.w(TAG, "unsubscribeFromTopic error", e);
result.error("unsubscribeFromTopic", e.getMessage(), null);
return;
}
result.success(null);
}
});
} else if ("getToken".equals(call.method)) {
FirebaseInstanceId.getInstance()
.getInstanceId()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ void main() {
expect(await firebaseMessaging.autoInitEnabled(), true);
});

// TODO(jackson): token retrieval isn't working on test devices yet
test('subscribeToTopic', () async {
firebaseMessaging.subscribeToTopic('foo');
});
await firebaseMessaging.subscribeToTopic('foo');
}, skip: true);

// TODO(jackson): token retrieval isn't working on test devices yet
test('unsubscribeFromTopic', () async {
firebaseMessaging.unsubscribeFromTopic('foo');
});
await firebaseMessaging.unsubscribeFromTopic('foo');
}, skip: true);

test('deleteInstanceID', () async {
final bool result = await firebaseMessaging.deleteInstanceID();
Expand Down
19 changes: 15 additions & 4 deletions packages/firebase_messaging/ios/Classes/FirebaseMessagingPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ @interface FLTFirebaseMessagingPlugin () <FIRMessagingDelegate>
@end
#endif

static FlutterError *getFlutterError(NSError *error) {
if (error == nil) return nil;
return [FlutterError errorWithCode:[NSString stringWithFormat:@"Error %ld", error.code]
message:error.domain
details:error.localizedDescription];
}

@implementation FLTFirebaseMessagingPlugin {
FlutterMethodChannel *_channel;
NSDictionary *_launchNotification;
Expand Down Expand Up @@ -77,12 +84,16 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
result(nil);
} else if ([@"subscribeToTopic" isEqualToString:method]) {
NSString *topic = call.arguments;
[[FIRMessaging messaging] subscribeToTopic:topic];
result(nil);
[[FIRMessaging messaging] subscribeToTopic:topic
completion:^(NSError *error) {
result(getFlutterError(error));
}];
} else if ([@"unsubscribeFromTopic" isEqualToString:method]) {
NSString *topic = call.arguments;
[[FIRMessaging messaging] unsubscribeFromTopic:topic];
result(nil);
[[FIRMessaging messaging] unsubscribeFromTopic:topic
completion:^(NSError *error) {
result(getFlutterError(error));
}];
} else if ([@"getToken" isEqualToString:method]) {
[[FIRInstanceID instanceID]
instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable instanceIDResult,
Expand Down
8 changes: 4 additions & 4 deletions packages/firebase_messaging/lib/firebase_messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ class FirebaseMessaging {
///
/// [topic] must match the following regular expression:
/// "[a-zA-Z0-9-_.~%]{1,900}".
void subscribeToTopic(String topic) {
_channel.invokeMethod<void>('subscribeToTopic', topic);
Future<void> subscribeToTopic(String topic) {
return _channel.invokeMethod<void>('subscribeToTopic', topic);
}

/// Unsubscribe from topic in background.
void unsubscribeFromTopic(String topic) {
_channel.invokeMethod<void>('unsubscribeFromTopic', topic);
Future<void> unsubscribeFromTopic(String topic) {
return _channel.invokeMethod<void>('unsubscribeFromTopic', topic);
}

/// Resets Instance ID and revokes all tokens. In iOS, it also unregisters from remote notifications.
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 <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_messaging
version: 5.0.6
version: 5.1.0

flutter:
plugin:
Expand Down
8 changes: 4 additions & 4 deletions packages/firebase_messaging/test/firebase_messaging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ void main() {

const String myTopic = 'Flutter';

test('subscribe to topic', () {
firebaseMessaging.subscribeToTopic(myTopic);
test('subscribe to topic', () async {
await firebaseMessaging.subscribeToTopic(myTopic);
verify(mockChannel.invokeMethod<void>('subscribeToTopic', myTopic));
});

test('unsubscribe from topic', () {
firebaseMessaging.unsubscribeFromTopic(myTopic);
test('unsubscribe from topic', () async {
await firebaseMessaging.unsubscribeFromTopic(myTopic);
verify(mockChannel.invokeMethod<void>('unsubscribeFromTopic', myTopic));
});

Expand Down