Skip to content

fix(messaging,nnbd): regression in RemoteMessage.fromMap() causing silent failure #5336

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

Merged
merged 10 commits into from
Mar 22, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class RemoteMessage {
senderId: map['senderId'],
category: map['category'],
collapseKey: map['collapseKey'],
contentAvailable: map['contentAvailable'],
contentAvailable: map['contentAvailable'] ?? false,
data: map['data'] == null
? <String, dynamic>{}
: Map<String, dynamic>.from(map['data']),
from: map['from'],
// Note: using toString on messageId as it can be an int or string when being sent from native.
messageId: map['messageId']?.toString(),
messageType: map['messageType'],
mutableContent: map['mutableContent'],
mutableContent: map['mutableContent'] ?? false,
notification: map['notification'] == null
? null
: RemoteNotification.fromMap(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:firebase_messaging_platform_interface/firebase_messaging_platform_interface.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
Map<String, dynamic>? mockMessageMap;

group('RemoteMessage: handle optional fields under sound null safety:', () {
setUp(() {
// basic message received, optional fields missing
mockMessageMap = {
'token': '012345678...',
'data': {
'via': 'FlutterFire Cloud Messaging!!!',
'count': 1,
},
'notification': {
'title': 'Hello FlutterFire!',
'body': 'This notification was created from unit tests!',
},
};
});

test('Optional fields should be set false if absent', () {
final message = RemoteMessage.fromMap(mockMessageMap!);

expect(message.data, mockMessageMap!['data']);
expect(message.notification, isA<RemoteNotification>());

expect(message.mutableContent, false);
expect(message.contentAvailable, false);
});

test('Optional fields should be set to given value if present', () {
// add values for optional fields
mockMessageMap!['mutableContent'] = true;
mockMessageMap!['contentAvailable'] = true;

final message = RemoteMessage.fromMap(mockMessageMap!);

expect(message.data, mockMessageMap!['data']);
expect(message.notification, isA<RemoteNotification>());

expect(message.mutableContent, true);
expect(message.contentAvailable, true);
});
});
}