Skip to content

Commit 7f9fdbf

Browse files
bkayfitz-carakroikie
authored andcommitted
Firebase Messaging: fix Android notification body and title not sent to Flutter (flutter#672)
* sending notification title and body to flutter * Safety checks for null pointer exceptions * Use remote data scheme on dart side
1 parent 7f8efac commit 7f9fdbf

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

packages/firebase_messaging/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.0
2+
3+
* Updated Android to send Remote Message's title and body to Dart.
4+
15
## 1.0.5
26

37
* Bumped test and mockito versions to pick up Dart 2 support.

packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.content.Intent;
1010
import android.content.IntentFilter;
1111
import android.os.Bundle;
12+
import android.support.annotation.NonNull;
1213
import android.support.v4.content.LocalBroadcastManager;
1314
import com.google.firebase.FirebaseApp;
1415
import com.google.firebase.messaging.FirebaseMessaging;
@@ -54,16 +55,41 @@ private FirebaseMessagingPlugin(Registrar registrar, MethodChannel channel) {
5455
@Override
5556
public void onReceive(Context context, Intent intent) {
5657
String action = intent.getAction();
58+
59+
if (action == null) {
60+
return;
61+
}
62+
5763
if (action.equals(FlutterFirebaseInstanceIDService.ACTION_TOKEN)) {
5864
String token = intent.getStringExtra(FlutterFirebaseInstanceIDService.EXTRA_TOKEN);
5965
channel.invokeMethod("onToken", token);
6066
} else if (action.equals(FlutterFirebaseMessagingService.ACTION_REMOTE_MESSAGE)) {
6167
RemoteMessage message =
6268
intent.getParcelableExtra(FlutterFirebaseMessagingService.EXTRA_REMOTE_MESSAGE);
63-
channel.invokeMethod("onMessage", message.getData());
69+
Map<String, Object> content = parseRemoteMessage(message);
70+
channel.invokeMethod("onMessage", content);
6471
}
6572
}
6673

74+
@NonNull
75+
private Map<String, Object> parseRemoteMessage(RemoteMessage message) {
76+
Map<String, Object> content = new HashMap<>();
77+
content.put("data", message.getData());
78+
79+
RemoteMessage.Notification notification = message.getNotification();
80+
81+
Map<String, Object> notificationMap = new HashMap<>();
82+
83+
String title = notification != null ? notification.getTitle() : null;
84+
notificationMap.put("title", title);
85+
86+
String body = notification != null ? notification.getBody() : null;
87+
notificationMap.put("body", body);
88+
89+
content.put("notification", notificationMap);
90+
return content;
91+
}
92+
6793
@Override
6894
public void onMethodCall(MethodCall call, Result result) {
6995
if ("configure".equals(call.method)) {
@@ -100,9 +126,18 @@ private boolean sendMessageFromIntent(String method, Intent intent) {
100126
|| CLICK_ACTION_VALUE.equals(intent.getStringExtra("click_action"))) {
101127
Map<String, String> message = new HashMap<>();
102128
Bundle extras = intent.getExtras();
129+
130+
if (extras == null) {
131+
return false;
132+
}
133+
103134
for (String key : extras.keySet()) {
104-
message.put(key, extras.get(key).toString());
135+
Object extra = extras.get(key);
136+
if (extra != null) {
137+
message.put(key, extra.toString());
138+
}
105139
}
140+
106141
channel.invokeMethod(method, message);
107142
return true;
108143
}

packages/firebase_messaging/example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import 'package:flutter/material.dart';
99

1010
final Map<String, Item> _items = <String, Item>{};
1111
Item _itemForMessage(Map<String, dynamic> message) {
12-
final String itemId = message['id'];
12+
final String itemId = message['data']['id'];
1313
final Item item = _items.putIfAbsent(itemId, () => new Item(itemId: itemId))
14-
..status = message['status'];
14+
..status = message['data']['status'];
1515
return item;
1616
}
1717

packages/firebase_messaging/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Flutter plugin for Firebase Cloud Messaging, a cross-platform
33
messaging solution that lets you reliably deliver messages on Android and iOS.
44
author: Flutter Team <flutter-dev@googlegroups.com>
55
homepage: https://github.com/flutter/plugins/tree/master/packages/firebase_messaging
6-
version: 1.0.5
6+
version: 2.0.0
77

88
flutter:
99
plugin:

0 commit comments

Comments
 (0)