Added a new field called push provider name in addDevice api call

This commit is contained in:
Ayush Shekhar
2022-04-27 18:23:17 +05:30
parent d58f727a57
commit 28649107ee
5 changed files with 83 additions and 6 deletions
@@ -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 ### Receiving Notifications
Push notifications behave a bit differently depending on whether you are using iOS or Android. Push notifications behave a bit differently depending on whether you are using iOS or Android.
@@ -818,8 +818,16 @@ class StreamChatClient {
); );
/// Add a device for Push Notifications. /// Add a device for Push Notifications.
Future<EmptyResponse> addDevice(String id, PushProvider pushProvider) => Future<EmptyResponse> addDevice(
_chatApi.device.addDevice(id, pushProvider); String id,
PushProvider pushProvider, {
String? pushProviderName,
}) =>
_chatApi.device.addDevice(
id,
pushProvider,
pushProviderName: pushProviderName,
);
/// Gets a list of user devices. /// Gets a list of user devices.
Future<ListDevicesResponse> getDevices() => _chatApi.device.getDevices(); Future<ListDevicesResponse> getDevices() => _chatApi.device.getDevices();
@@ -29,13 +29,16 @@ class DeviceApi {
/// Add a device for Push Notifications. /// Add a device for Push Notifications.
Future<EmptyResponse> addDevice( Future<EmptyResponse> addDevice(
String deviceId, String deviceId,
PushProvider pushProvider, PushProvider pushProvider, {
) async { String? pushProviderName,
}) async {
final response = await _client.post( final response = await _client.post(
'/devices', '/devices',
data: { data: {
'id': deviceId, 'id': deviceId,
'push_provider': pushProvider.name, 'push_provider': pushProvider.name,
if (pushProviderName != null && pushProviderName.isNotEmpty)
'push_provider_name': pushProviderName,
}, },
); );
return EmptyResponse.fromJson(response.data); return EmptyResponse.fromJson(response.data);
@@ -1171,7 +1171,7 @@ void main() {
verifyNoMoreInteractions(api.channel); verifyNoMoreInteractions(api.channel);
}); });
test('`.addDevice`', () async { test('`.addDevice should work`', () async {
const id = 'test-device-id'; const id = 'test-device-id';
const provider = PushProvider.firebase; const provider = PushProvider.firebase;
@@ -1185,6 +1185,34 @@ void main() {
verifyNoMoreInteractions(api.device); 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 { test('`.getDevices`', () async {
final devices = List.generate( final devices = List.generate(
3, 3,
@@ -20,7 +20,7 @@ void main() {
deviceApi = DeviceApi(client); deviceApi = DeviceApi(client);
}); });
test('addDevice', () async { test('addDevice should work', () async {
const deviceId = 'test-device-id'; const deviceId = 'test-device-id';
const pushProvider = PushProvider.firebase; const pushProvider = PushProvider.firebase;
@@ -44,6 +44,36 @@ void main() {
verifyNoMoreInteractions(client); 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: <String, dynamic>{}));
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 { test('getDevices', () async {
const path = '/devices'; const path = '/devices';