1818
1919package im .vector .gcm ;
2020
21+ import android .support .annotation .Nullable ;
22+
2123import com .google .firebase .messaging .FirebaseMessagingService ;
2224import com .google .firebase .messaging .RemoteMessage ;
2325import com .google .gson .JsonParser ;
@@ -54,6 +56,7 @@ public class MatrixGcmListenerService extends FirebaseMessagingService {
5456 * @param data the GCM data
5557 * @return the event
5658 */
59+ @ Nullable
5760 private Event parseEvent (Map <String , String > data ) {
5861 // accept only event with room id.
5962 if ((null == data ) || !data .containsKey ("room_id" ) || !data .containsKey ("event_id" )) {
@@ -73,7 +76,7 @@ private Event parseEvent(Map<String, String> data) {
7376
7477 return event ;
7578 } catch (Exception e ) {
76- Log .e (LOG_TAG , "buildEvent fails " + e .getLocalizedMessage ());
79+ Log .e (LOG_TAG , "buildEvent fails " + e .getLocalizedMessage (), e );
7780 }
7881
7982 return null ;
@@ -121,13 +124,14 @@ private void onMessageReceivedInternal(final Map<String, String> data) {
121124 Event event = parseEvent (data );
122125
123126 String roomName = data .get ("room_name" );
124- if ((null == roomName ) && (null != roomId )) {
127+ if (null == roomName && null != roomId ) {
128+ // Try to get the room name from our store
125129 MXSession session = Matrix .getInstance (getApplicationContext ()).getDefaultSession ();
126130
127131 if ((null != session ) && session .getDataHandler ().getStore ().isReady ()) {
128132 Room room = session .getDataHandler ().getStore ().getRoom (roomId );
129133 if (null != room ) {
130- roomName = VectorUtils .getRoomDisplayName (MatrixGcmListenerService . this , session , room );
134+ roomName = VectorUtils .getRoomDisplayName (this , session , room );
131135 }
132136 }
133137 }
@@ -141,36 +145,36 @@ private void onMessageReceivedInternal(final Map<String, String> data) {
141145 // check if the application has been launched once
142146 // the first GCM event could have been triggered whereas the application is not yet launched.
143147 // so it is required to create the sessions and to start/resume event stream
144- if (!mCheckLaunched && ( null != Matrix .getInstance (getApplicationContext ()).getDefaultSession () )) {
145- CommonActivityUtils .startEventStreamService (MatrixGcmListenerService . this );
148+ if (!mCheckLaunched && null != Matrix .getInstance (getApplicationContext ()).getDefaultSession ()) {
149+ CommonActivityUtils .startEventStreamService (this );
146150 mCheckLaunched = true ;
147151 }
148152
149153 // check if the event was not yet received
150154 // a previous catchup might have already retrieved the notified event
151- if (( null != eventId ) && ( null != roomId ) ) {
155+ if (null != eventId && null != roomId ) {
152156 try {
153157 Collection <MXSession > sessions = Matrix .getInstance (getApplicationContext ()).getSessions ();
154158
155- if (( null != sessions ) && ( sessions .size () > 0 )) {
159+ if (null != sessions && ! sessions .isEmpty ( )) {
156160 for (MXSession session : sessions ) {
157161 if (session .getDataHandler ().getStore ().isReady ()) {
158162 if (null != session .getDataHandler ().getStore ().getEvent (eventId , roomId )) {
159163 Log .e (LOG_TAG , "## onMessageReceivedInternal() : ignore the event " + eventId
160- + " in room " + roomId + "because it is already known" );
164+ + " in room " + roomId + " because it is already known" );
161165 return ;
162166 }
163167 }
164168 }
165169 }
166170 } catch (Exception e ) {
167- Log .e (LOG_TAG , "## onMessageReceivedInternal() : failed to check if the event was already defined " + e .getMessage ());
171+ Log .e (LOG_TAG , "## onMessageReceivedInternal() : failed to check if the event was already defined " + e .getMessage (), e );
168172 }
169173 }
170174
171- CommonActivityUtils .catchupEventStream (MatrixGcmListenerService . this );
175+ CommonActivityUtils .catchupEventStream (this );
172176 } catch (Exception e ) {
173- Log .d (LOG_TAG , "## onMessageReceivedInternal() failed : " + e .getMessage ());
177+ Log .d (LOG_TAG , "## onMessageReceivedInternal() failed : " + e .getMessage (), e );
174178 }
175179 }
176180
@@ -181,6 +185,13 @@ private void onMessageReceivedInternal(final Map<String, String> data) {
181185 */
182186 @ Override
183187 public void onMessageReceived (RemoteMessage message ) {
188+ Log .d (LOG_TAG , "## onMessageReceived() from FCM" );
189+
190+ // Ensure event stream service is started
191+ if (EventStreamService .getInstance () == null ) {
192+ CommonActivityUtils .startEventStreamService (this );
193+ }
194+
184195 final Map <String , String > data = message .getData ();
185196
186197 if (null == mUIHandler ) {
0 commit comments