Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Fix notification issue when Riot is not started #2451

Merged
merged 9 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Bugfix:
- Fix issue on verifying device, update the wording of the description message (#1067)
- Messages with code blocks show other HTML as plain text (#2280)
- Message with <p> was sometimes not properly formatted (#2275)
- Fix notification issue when Riot is not started (#2451)

Translations:
-
Expand Down
27 changes: 19 additions & 8 deletions vector/src/app/java/im/vector/gcm/MatrixGcmListenerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package im.vector.gcm;

import android.support.annotation.Nullable;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.google.gson.JsonParser;
Expand Down Expand Up @@ -54,6 +56,7 @@ public class MatrixGcmListenerService extends FirebaseMessagingService {
* @param data the GCM data
* @return the event
*/
@Nullable
private Event parseEvent(Map<String, String> data) {
// accept only event with room id.
if ((null == data) || !data.containsKey("room_id") || !data.containsKey("event_id")) {
Expand Down Expand Up @@ -121,13 +124,14 @@ private void onMessageReceivedInternal(final Map<String, String> data) {
Event event = parseEvent(data);

String roomName = data.get("room_name");
if ((null == roomName) && (null != roomId)) {
if (null == roomName && null != roomId) {
// Try to get the room name from our store
MXSession session = Matrix.getInstance(getApplicationContext()).getDefaultSession();

if ((null != session) && session.getDataHandler().getStore().isReady()) {
Room room = session.getDataHandler().getStore().getRoom(roomId);
if (null != room) {
roomName = VectorUtils.getRoomDisplayName(MatrixGcmListenerService.this, session, room);
roomName = VectorUtils.getRoomDisplayName(this, session, room);
}
}
}
Expand All @@ -141,23 +145,23 @@ private void onMessageReceivedInternal(final Map<String, String> data) {
// check if the application has been launched once
// the first GCM event could have been triggered whereas the application is not yet launched.
// so it is required to create the sessions and to start/resume event stream
if (!mCheckLaunched && (null != Matrix.getInstance(getApplicationContext()).getDefaultSession())) {
CommonActivityUtils.startEventStreamService(MatrixGcmListenerService.this);
if (!mCheckLaunched && null != Matrix.getInstance(getApplicationContext()).getDefaultSession()) {
CommonActivityUtils.startEventStreamService(this);
mCheckLaunched = true;
}

// check if the event was not yet received
// a previous catchup might have already retrieved the notified event
if ((null != eventId) && (null != roomId)) {
if (null != eventId && null != roomId) {
try {
Collection<MXSession> sessions = Matrix.getInstance(getApplicationContext()).getSessions();

if ((null != sessions) && (sessions.size() > 0)) {
if (null != sessions && !sessions.isEmpty()) {
for (MXSession session : sessions) {
if (session.getDataHandler().getStore().isReady()) {
if (null != session.getDataHandler().getStore().getEvent(eventId, roomId)) {
Log.e(LOG_TAG, "## onMessageReceivedInternal() : ignore the event " + eventId
+ " in room " + roomId + "because it is already known");
+ " in room " + roomId + " because it is already known");
return;
}
}
Expand All @@ -168,7 +172,7 @@ private void onMessageReceivedInternal(final Map<String, String> data) {
}
}

CommonActivityUtils.catchupEventStream(MatrixGcmListenerService.this);
CommonActivityUtils.catchupEventStream(this);
} catch (Exception e) {
Log.d(LOG_TAG, "## onMessageReceivedInternal() failed : " + e.getMessage(), e);
}
Expand All @@ -181,6 +185,13 @@ private void onMessageReceivedInternal(final Map<String, String> data) {
*/
@Override
public void onMessageReceived(RemoteMessage message) {
Log.d(LOG_TAG, "## onMessageReceived() from FCM");

// Ensure event stream service is started
if (EventStreamService.getInstance() == null) {
CommonActivityUtils.startEventStreamService(this);
}

final Map<String, String> data = message.getData();

if (null == mUIHandler) {
Expand Down
2 changes: 1 addition & 1 deletion vector/src/main/java/im/vector/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ private Matrix(Context appContext) {
* @return the shared instance
*/
public synchronized static Matrix getInstance(Context appContext) {
if ((instance == null) && (null != appContext)) {
if (instance == null && null != appContext) {
instance = new Matrix(appContext);
}
return instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ public static void startEventStreamService(Context context) {
}
}

if (null != EventStreamService.getInstance()) {
if (EventStreamService.getInstance() != null) {
EventStreamService.getInstance().refreshForegroundNotification();
}
}
Expand Down
2 changes: 1 addition & 1 deletion vector/src/main/java/im/vector/activity/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public void initUiAndData() {
goToSplash();
} else {
// detect if the application has already been started
if (null == EventStreamService.getInstance()) {
if (EventStreamService.getInstance() == null) {
Log.d(LOG_TAG, "## onCreate(): goToSplash with credentials but there is no event stream service.");
goToSplash();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import org.jetbrains.annotations.NotNull;

import butterknife.BindView;
import butterknife.OnClick;
import im.vector.Matrix;
import im.vector.R;
import im.vector.gcm.GcmRegistrationManager;
Expand All @@ -40,25 +40,20 @@
* The interest is to educate the user on the impacts of his choice of the type of notifications
* on the privacy policy of his data.
*/
public class NotificationPrivacyActivity extends RiotAppCompatActivity {
public class NotificationPrivacyActivity extends RiotAppCompatActivity {

private static final String LOG_TAG = NotificationPrivacyActivity.class.getSimpleName();

/* ==========================================================================================
* UI
* ========================================================================================== */

@BindView(R.id.tv_apps_needs_permission)
TextView tvNeedPermission;

@BindView(R.id.tv_apps_no_permission)
TextView tvNoPermission;

@BindView(R.id.rly_normal_notification_privacy)
View rlyNormalPrivacy;

@BindView(R.id.rly_low_detail_notifications)
View rlyLowDetailNotifications;

@BindView(R.id.rly_reduced_privacy_notifications)
View rlyReducedPrivacy;

@BindView(R.id.rb_normal_notification_privacy)
RadioButton rbPrivacyNormal;

Expand All @@ -68,18 +63,9 @@ public class NotificationPrivacyActivity extends RiotAppCompatActivity {
@BindView(R.id.rb_notification_reduce_privacy)
RadioButton rbPrivacyReduced;

@BindView(R.id.tv_normal_notification_privacy)
TextView tvPrivacyNormal;

@BindView(R.id.tv_notification_low_detail)
TextView tvPrivacyLowDetail;

@BindView(R.id.tv_notification_reduce_privacy)
TextView tvPrivacyReduced;

public static Intent getIntent(final Context context) {
return new Intent(context, NotificationPrivacyActivity.class);
}
/* ==========================================================================================
* LifeCycle
* ========================================================================================== */

@NotNull
@Override
Expand All @@ -99,90 +85,88 @@ public int getTitleRes() {

@Override
public void initUiAndData() {
Toolbar toolbar = findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
configureToolbar();

// The permission request is only necessary for devices os versions greater than API 23 (M)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
tvNeedPermission.setVisibility(View.VISIBLE);
tvNoPermission.setVisibility(View.VISIBLE);
} else{
} else {
tvNeedPermission.setVisibility(View.GONE);
tvNoPermission.setVisibility(View.GONE);
}

refreshNotificationPrivacy();

rlyNormalPrivacy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setNotificationPrivacy(NotificationPrivacyActivity.this, GcmRegistrationManager.NotificationPrivacy.NORMAL);
refreshNotificationPrivacy();
}
});

rlyLowDetailNotifications.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setNotificationPrivacy(NotificationPrivacyActivity.this, GcmRegistrationManager.NotificationPrivacy.LOW_DETAIL);
refreshNotificationPrivacy();
}
});

rlyReducedPrivacy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setNotificationPrivacy(NotificationPrivacyActivity.this, GcmRegistrationManager.NotificationPrivacy.REDUCED);
refreshNotificationPrivacy();
}
});
}

@Override
protected void onResume() {
super.onResume();

refreshNotificationPrivacy();
}

/* ==========================================================================================
* UI Event
* ========================================================================================== */

@OnClick(R.id.rly_normal_notification_privacy)
void onNormalClick() {
updateNotificationPrivacy(GcmRegistrationManager.NotificationPrivacy.NORMAL);
}

@OnClick(R.id.rly_low_detail_notifications)
void onLowDetailClick() {
updateNotificationPrivacy(GcmRegistrationManager.NotificationPrivacy.LOW_DETAIL);
}

@OnClick(R.id.rly_reduced_privacy_notifications)
void onReducedPrivacyClick() {
updateNotificationPrivacy(GcmRegistrationManager.NotificationPrivacy.REDUCED);
}

/* ==========================================================================================
* Private
* ========================================================================================== */

private void updateNotificationPrivacy(GcmRegistrationManager.NotificationPrivacy newNotificationPrivacy) {
setNotificationPrivacy(this, newNotificationPrivacy);
refreshNotificationPrivacy();
}

private void refreshNotificationPrivacy() {
GcmRegistrationManager gcmRegistrationManager = Matrix.getInstance(this).getSharedGCMRegistrationManager();
GcmRegistrationManager.NotificationPrivacy notificationPrivacy = Matrix.getInstance(this)
.getSharedGCMRegistrationManager()
.getNotificationPrivacy();

switch (gcmRegistrationManager.getNotificationPrivacy()) {
case REDUCED:
rbPrivacyNormal.setChecked(false);
rbPrivacyLowDetail.setChecked(false);
rbPrivacyReduced.setChecked(true);
break;
case LOW_DETAIL:
rbPrivacyNormal.setChecked(false);
rbPrivacyLowDetail.setChecked(true);
rbPrivacyReduced.setChecked(false);
break;
case NORMAL:
rbPrivacyNormal.setChecked(true);
rbPrivacyLowDetail.setChecked(false);
rbPrivacyReduced.setChecked(false);
break;
}
rbPrivacyNormal.setChecked(notificationPrivacy == GcmRegistrationManager.NotificationPrivacy.NORMAL);
rbPrivacyLowDetail.setChecked(notificationPrivacy == GcmRegistrationManager.NotificationPrivacy.LOW_DETAIL);
rbPrivacyReduced.setChecked(notificationPrivacy == GcmRegistrationManager.NotificationPrivacy.REDUCED);
}

/* ==========================================================================================
* Public static
* ========================================================================================== */

/**
* Return an intent to start this Activity
*
* @param context Android context
* @return an intent to start this Activity
*/
public static Intent getIntent(final Context context) {
return new Intent(context, NotificationPrivacyActivity.class);
}

/**
* Set the new notification privacy setting.
*
* @param activity the activity from which to display the IgnoreBatteryOptimizations permission request dialog, if required
* @param activity the activity from which to display the IgnoreBatteryOptimizations permission request dialog, if required
* @param notificationPrivacy the new setting
*/
static public void setNotificationPrivacy(Activity activity, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) {
GcmRegistrationManager gcmRegistrationManager = Matrix.getInstance(activity).getSharedGCMRegistrationManager();

public static void setNotificationPrivacy(Activity activity, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) {
// first, set the new privacy setting
gcmRegistrationManager.setNotificationPrivacy(notificationPrivacy);
Matrix.getInstance(activity)
.getSharedGCMRegistrationManager()
.setNotificationPrivacy(notificationPrivacy);

// for the "NORMAL" privacy, the app needs to be able to run in background
// this requires the IgnoreBatteryOptimizations permission from android M
Expand All @@ -201,25 +185,27 @@ static public void setNotificationPrivacy(Activity activity, GcmRegistrationMana

/**
* Get the displayed i18ned string for a notification privacy setting.
*
* @param context
*
* @param context Android context
* @param notificationPrivacy the setting to stringify
* @return a string
*/
static public String getNotificationPrivacyString(Context context, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) {
String notificationPrivacyString = null;
public static String getNotificationPrivacyString(Context context, GcmRegistrationManager.NotificationPrivacy notificationPrivacy) {
int stringRes;

switch (notificationPrivacy) {
case REDUCED:
notificationPrivacyString = context.getString(R.string.settings_notification_privacy_reduced);
stringRes = R.string.settings_notification_privacy_reduced;
break;
case LOW_DETAIL:
notificationPrivacyString = context.getString(R.string.settings_notification_privacy_low_detail);
stringRes = R.string.settings_notification_privacy_low_detail;
break;
case NORMAL:
notificationPrivacyString = context.getString(R.string.settings_notification_privacy_normal);
break;}
default:
stringRes = R.string.settings_notification_privacy_normal;
break;
}

return notificationPrivacyString;
return context.getString(stringRes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public void onInitialSyncComplete(String toToken) {
// trigger the GCM registration if required
GcmRegistrationManager gcmRegistrationManager = Matrix.getInstance(getApplicationContext()).getSharedGCMRegistrationManager();

if (!gcmRegistrationManager.isGCMRegistred()) {
if (!gcmRegistrationManager.isGcmRegistered()) {
gcmRegistrationManager.checkRegistrations();
} else {
gcmRegistrationManager.forceSessionsRegistration(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1817,7 +1817,7 @@ public void onDrawerClosed(View view) {
}

case R.id.sliding_menu_exit: {
if (null != EventStreamService.getInstance()) {
if (EventStreamService.getInstance() != null) {
EventStreamService.getInstance().stopNow();
}
runOnUiThread(new Runnable() {
Expand Down
Loading