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

feat(firebase_messaging, web): Added serviceWorkerRegistration to GetTokenOptions #17151

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -116,9 +116,11 @@ class FirebaseMessaging extends FirebasePluginPlatform {
/// On web, a [vapidKey] is required.
Future<String?> getToken({
String? vapidKey,
Object? serviceWorkerRegistration,
}) {
return _delegate.getToken(
vapidKey: vapidKey,
serviceWorkerRegistration: serviceWorkerRegistration,
);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/firebase_messaging/firebase_messaging/test/mock.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ class MockFirebaseMessaging extends Mock
}

@override
Future<String> getToken({String? vapidKey}) {
Future<String> getToken({String? vapidKey, Object? serviceWorkerRegistration}) {
return super.noSuchMethod(
Invocation.method(#getToken, [], {#vapidKey: vapidKey}),
Invocation.method(#getToken, [], {#vapidKey: vapidKey, #serviceWorkerRegistration: serviceWorkerRegistration}),
returnValue: Future<String>.value(''),
returnValueForMissingStub: Future<String>.value(''));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class MethodChannelFirebaseMessaging extends FirebaseMessagingPlatform {
@override
Future<String?> getToken({
String? vapidKey, // not used yet; web only property
Object? serviceWorkerRegistration, // not used yet; web only property
}) async {
await _APNSTokenCheck();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ abstract class FirebaseMessagingPlatform extends PlatformInterface {
/// Returns the default FCM token for this device and optionally [senderId].
Future<String?> getToken({
String? vapidKey,
// Should always be a ServiceWorkerRegistration
Object? serviceWorkerRegistration,
}) {
throw UnimplementedError('getToken() is not implemented');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class FirebaseMessagingWeb extends FirebaseMessagingPlatform {
}

@override
Future<String?> getToken({String? vapidKey}) async {
Future<String?> getToken({String? vapidKey, Object? serviceWorkerRegistration}) async {
assert(serviceWorkerRegistration is web.ServiceWorkerRegistration);
_delegate;

if (!_initialized) {
Expand All @@ -115,7 +116,7 @@ class FirebaseMessagingWeb extends FirebaseMessagingPlatform {
}

return convertWebExceptions(
() => _delegate.getToken(vapidKey: vapidKey),
() => _delegate.getToken(vapidKey: vapidKey, serviceWorkerRegistration: serviceWorkerRegistration as web.ServiceWorkerRegistration?),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'dart:async';
import 'dart:js_interop';

import 'package:firebase_core_web/firebase_core_web_interop.dart';
import 'package:web/web.dart' as web;

import 'messaging_interop.dart' as messaging_interop;

Expand Down Expand Up @@ -43,15 +44,16 @@ class Messaging extends JsObjectWrapper<messaging_interop.MessagingJsImpl> {

/// After calling [requestPermission] you can call this method to get an FCM registration token
/// that can be used to send push messages to this user.
Future<String> getToken({String? vapidKey}) async {
Future<String> getToken({String? vapidKey, web.ServiceWorkerRegistration? serviceWorkerRegistration}) async {
try {
final token = (await messaging_interop
.getToken(
jsObject,
vapidKey == null
? null
: messaging_interop.GetTokenOptions(
vapidKey: vapidKey.toJS))
vapidKey: vapidKey.toJS,
serviceWorkerRegistration: serviceWorkerRegistration))
.toDart)
.toDart;
return token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ library;
import 'dart:js_interop';

import 'package:firebase_core_web/firebase_core_web_interop.dart';
import 'package:web/web.dart' as web;

@JS()
@staticInterop
Expand Down Expand Up @@ -56,11 +57,9 @@ extension ObserverJsImplX on Observer {
@staticInterop
@anonymous
class GetTokenOptions {
// TODO - I imagine we won't be implementing serviceWorkerRegistration type as it extends EventTarget class
// external String get serviceWorkerRegistration
external factory GetTokenOptions({
JSString? vapidKey,
/*dynamic serviceWorkerRegistration */
web.ServiceWorkerRegistration? serviceWorkerRegistration
});
}

Expand Down