|
9 | 9 | import android.content.Intent;
|
10 | 10 | import android.content.IntentFilter;
|
11 | 11 | import android.os.Bundle;
|
| 12 | +import android.support.annotation.NonNull; |
12 | 13 | import android.support.v4.content.LocalBroadcastManager;
|
13 | 14 | import com.google.firebase.FirebaseApp;
|
14 | 15 | import com.google.firebase.messaging.FirebaseMessaging;
|
@@ -54,16 +55,41 @@ private FirebaseMessagingPlugin(Registrar registrar, MethodChannel channel) {
|
54 | 55 | @Override
|
55 | 56 | public void onReceive(Context context, Intent intent) {
|
56 | 57 | String action = intent.getAction();
|
| 58 | + |
| 59 | + if (action == null) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
57 | 63 | if (action.equals(FlutterFirebaseInstanceIDService.ACTION_TOKEN)) {
|
58 | 64 | String token = intent.getStringExtra(FlutterFirebaseInstanceIDService.EXTRA_TOKEN);
|
59 | 65 | channel.invokeMethod("onToken", token);
|
60 | 66 | } else if (action.equals(FlutterFirebaseMessagingService.ACTION_REMOTE_MESSAGE)) {
|
61 | 67 | RemoteMessage message =
|
62 | 68 | intent.getParcelableExtra(FlutterFirebaseMessagingService.EXTRA_REMOTE_MESSAGE);
|
63 |
| - channel.invokeMethod("onMessage", message.getData()); |
| 69 | + Map<String, Object> content = parseRemoteMessage(message); |
| 70 | + channel.invokeMethod("onMessage", content); |
64 | 71 | }
|
65 | 72 | }
|
66 | 73 |
|
| 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 | + |
67 | 93 | @Override
|
68 | 94 | public void onMethodCall(MethodCall call, Result result) {
|
69 | 95 | if ("configure".equals(call.method)) {
|
@@ -100,9 +126,18 @@ private boolean sendMessageFromIntent(String method, Intent intent) {
|
100 | 126 | || CLICK_ACTION_VALUE.equals(intent.getStringExtra("click_action"))) {
|
101 | 127 | Map<String, String> message = new HashMap<>();
|
102 | 128 | Bundle extras = intent.getExtras();
|
| 129 | + |
| 130 | + if (extras == null) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
103 | 134 | 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 | + } |
105 | 139 | }
|
| 140 | + |
106 | 141 | channel.invokeMethod(method, message);
|
107 | 142 | return true;
|
108 | 143 | }
|
|
0 commit comments