diff --git a/docusaurus/docs/Flutter/guides/adding_push_notifications_v2.mdx b/docusaurus/docs/Flutter/guides/adding_push_notifications_v2.mdx index 285c2205..c65b9136 100644 --- a/docusaurus/docs/Flutter/guides/adding_push_notifications_v2.mdx +++ b/docusaurus/docs/Flutter/guides/adding_push_notifications_v2.mdx @@ -86,6 +86,14 @@ firebaseMessaging.onTokenRefresh.listen((token) { }); ``` +Push Notifications v2 also supports specifying a name to the push tokens you register. By setting the optional `pushProviderName` param in the `addDevice` call you can support different configurations between and the device and the `PushProvider`. + +```dart +firebaseMessaging.onTokenRefresh.listen((token) { + client.addDevice(token, PushProvider.firebase, pushProviderName: 'my-custom-config'); +}); +``` + ### Receiving Notifications Push notifications behave a bit differently depending on whether you are using iOS or Android. diff --git a/packages/stream_chat/lib/src/client/client.dart b/packages/stream_chat/lib/src/client/client.dart index 18254389..69fd3180 100644 --- a/packages/stream_chat/lib/src/client/client.dart +++ b/packages/stream_chat/lib/src/client/client.dart @@ -818,8 +818,16 @@ class StreamChatClient { ); /// Add a device for Push Notifications. - Future addDevice(String id, PushProvider pushProvider) => - _chatApi.device.addDevice(id, pushProvider); + Future addDevice( + String id, + PushProvider pushProvider, { + String? pushProviderName, + }) => + _chatApi.device.addDevice( + id, + pushProvider, + pushProviderName: pushProviderName, + ); /// Gets a list of user devices. Future getDevices() => _chatApi.device.getDevices(); diff --git a/packages/stream_chat/lib/src/core/api/device_api.dart b/packages/stream_chat/lib/src/core/api/device_api.dart index f4b7f0b1..df137cdc 100644 --- a/packages/stream_chat/lib/src/core/api/device_api.dart +++ b/packages/stream_chat/lib/src/core/api/device_api.dart @@ -29,13 +29,16 @@ class DeviceApi { /// Add a device for Push Notifications. Future addDevice( String deviceId, - PushProvider pushProvider, - ) async { + PushProvider pushProvider, { + String? pushProviderName, + }) async { final response = await _client.post( '/devices', data: { 'id': deviceId, 'push_provider': pushProvider.name, + if (pushProviderName != null && pushProviderName.isNotEmpty) + 'push_provider_name': pushProviderName, }, ); return EmptyResponse.fromJson(response.data); diff --git a/packages/stream_chat/test/src/client/client_test.dart b/packages/stream_chat/test/src/client/client_test.dart index 5ec2bfeb..4947bc72 100644 --- a/packages/stream_chat/test/src/client/client_test.dart +++ b/packages/stream_chat/test/src/client/client_test.dart @@ -1171,7 +1171,7 @@ void main() { verifyNoMoreInteractions(api.channel); }); - test('`.addDevice`', () async { + test('`.addDevice should work`', () async { const id = 'test-device-id'; const provider = PushProvider.firebase; @@ -1185,6 +1185,34 @@ void main() { verifyNoMoreInteractions(api.device); }); + test('`.addDevice should work with pushProviderName`', () async { + const id = 'test-device-id'; + const provider = PushProvider.firebase; + const pushProviderName = 'my-custom-config'; + + when( + () => api.device.addDevice( + id, + provider, + pushProviderName: pushProviderName, + ), + ).thenAnswer((_) async => EmptyResponse()); + + final res = await client.addDevice( + id, + provider, + pushProviderName: pushProviderName, + ); + expect(res, isNotNull); + + verify(() => api.device.addDevice( + id, + provider, + pushProviderName: pushProviderName, + )).called(1); + verifyNoMoreInteractions(api.device); + }); + test('`.getDevices`', () async { final devices = List.generate( 3, diff --git a/packages/stream_chat/test/src/core/api/device_api_test.dart b/packages/stream_chat/test/src/core/api/device_api_test.dart index 7d4f59fd..13cfe4dc 100644 --- a/packages/stream_chat/test/src/core/api/device_api_test.dart +++ b/packages/stream_chat/test/src/core/api/device_api_test.dart @@ -20,7 +20,7 @@ void main() { deviceApi = DeviceApi(client); }); - test('addDevice', () async { + test('addDevice should work', () async { const deviceId = 'test-device-id'; const pushProvider = PushProvider.firebase; @@ -44,6 +44,36 @@ void main() { verifyNoMoreInteractions(client); }); + test('addDevice should work with pushProviderName', () async { + const deviceId = 'test-device-id'; + const pushProvider = PushProvider.firebase; + const pushProviderName = 'my-custom-config'; + + const path = '/devices'; + + when(() => client.post( + path, + data: { + 'id': deviceId, + 'push_provider': pushProvider.name, + 'push_provider_name': pushProviderName, + }, + )) + .thenAnswer( + (_) async => successResponse(path, data: {})); + + final res = await deviceApi.addDevice( + deviceId, + pushProvider, + pushProviderName: pushProviderName, + ); + + expect(res, isNotNull); + + verify(() => client.post(path, data: any(named: 'data'))).called(1); + verifyNoMoreInteractions(client); + }); + test('getDevices', () async { const path = '/devices';