Skip to content

[firebase_messaging] Add support for stable Instance ID. #2328

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

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,22 @@ public void onComplete(@NonNull Task<InstanceIdResult> task) {
result.success(task.getResult().getToken());
}
});
} else if ("getInstanceID".equals(call.method)) {
FirebaseInstanceId.getInstance()
.getInstanceId()
.addOnCompleteListener(
new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(TAG, "getInstanceID, error fetching instanceID: ", task.getException());
result.success(null);
return;
}

result.success(task.getResult().getId());
}
});
} else if ("deleteInstanceID".equals(call.method)) {
new Thread(
new Runnable() {
Expand Down
19 changes: 15 additions & 4 deletions packages/firebase_messaging/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class PushMessagingExample extends StatefulWidget {
}

class _PushMessagingExampleState extends State<PushMessagingExample> {
String _homeScreenText = "Waiting for token...";
String _idStatusText = "Waiting for ID...";
String _tokenStatusText = "Waiting for token...";
bool _topicButtonsDisabled = false;

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
Expand Down Expand Up @@ -159,12 +160,19 @@ class _PushMessagingExampleState extends State<PushMessagingExample> {
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.getId().then((id) {
assert(id != null);
setState(() {
_idStatusText = "Instance ID: $id";
});
print(_idStatusText);
});
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
setState(() {
_homeScreenText = "Push Messaging token: $token";
_tokenStatusText = "Push Messaging token: $token";
});
print(_homeScreenText);
print(_tokenStatusText);
});
}

Expand All @@ -189,7 +197,10 @@ class _PushMessagingExampleState extends State<PushMessagingExample> {
child: Column(
children: <Widget>[
Center(
child: Text(_homeScreenText),
child: Text(_idStatusText),
),
Center(
child: Text(_tokenStatusText),
),
Row(children: <Widget>[
Expanded(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
result(instanceIDResult.token);
}
}];
} else if ([@"getInstanceID" isEqualToString:method]) {
[[FIRInstanceID instanceID]
instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable instanceIDResult,
NSError *_Nullable error) {
if (error != nil) {
NSLog(@"getInstanceID, error fetching instanceID: %@", error);
result(nil);
} else {
result(instanceIDResult.instanceID);
}
}];
} else if ([@"deleteInstanceID" isEqualToString:method]) {
[[FIRInstanceID instanceID] deleteIDWithHandler:^void(NSError *_Nullable error) {
if (error.code != 0) {
Expand Down
5 changes: 5 additions & 0 deletions packages/firebase_messaging/lib/firebase_messaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ class FirebaseMessaging {
return await _channel.invokeMethod<String>('getToken');
}

/// Returns the FCM stable ID.
Future<String> getId() async {
return await _channel.invokeMethod<String>('getInstanceID');
}

/// Subscribe to topic in background.
///
/// [topic] must match the following regular expression:
Expand Down
5 changes: 5 additions & 0 deletions packages/firebase_messaging/test/firebase_messaging_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ void main() {
verify(mockChannel.invokeMethod<void>('unsubscribeFromTopic', myTopic));
});

test('getId', () {
firebaseMessaging.getId();
verify(mockChannel.invokeMethod<String>('getInstanceID'));
});

test('getToken', () {
firebaseMessaging.getToken();
verify(mockChannel.invokeMethod<String>('getToken'));
Expand Down