Prevent double notifications

Check timelines for updates on resume
This commit is contained in:
Eugen Rochko 2017-03-12 08:49:38 +01:00
parent 2bbd46e841
commit daf7e6ed6a
4 changed files with 50 additions and 15 deletions

View File

@ -109,7 +109,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
} }
private void buildNotification(Notification body) { private void buildNotification(Notification body) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Intent resultIntent = new Intent(this, MainActivity.class); Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("tab_position", 1); resultIntent.putExtra("tab_position", 1);
@ -121,7 +121,8 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notify) .setSmallIcon(R.drawable.ic_notify)
.setAutoCancel(true) .setAutoCancel(true)
.setContentIntent(resultPendingIntent); .setContentIntent(resultPendingIntent)
.setDefaults(0); // So it doesn't ring twice, notify only in Target callback
final Integer mId = (int)(System.currentTimeMillis() / 1000); final Integer mId = (int)(System.currentTimeMillis() / 1000);
@ -129,6 +130,19 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override @Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
builder.setLargeIcon(bitmap); builder.setLargeIcon(bitmap);
if (preferences.getBoolean("notificationAlertSound", true)) {
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
}
if (preferences.getBoolean("notificationStyleVibrate", false)) {
builder.setVibrate(new long[] { 500, 500 });
}
if (preferences.getBoolean("notificationStyleLight", false)) {
builder.setLights(0xFF00FF8F, 300, 1000);
}
((NotificationManager) (getSystemService(NOTIFICATION_SERVICE))).notify(mId, builder.build()); ((NotificationManager) (getSystemService(NOTIFICATION_SERVICE))).notify(mId, builder.build());
} }
@ -148,18 +162,6 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
.placeholder(R.drawable.avatar_default) .placeholder(R.drawable.avatar_default)
.into(mTarget); .into(mTarget);
if (preferences.getBoolean("notificationAlertSound", true)) {
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
}
if (preferences.getBoolean("notificationStyleVibrate", false)) {
builder.setVibrate(new long[] { 500, 500 });
}
if (preferences.getBoolean("notificationStyleLight", false)) {
builder.setLights(0xFF00FF8F, 300, 1000);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setVisibility(android.app.Notification.VISIBILITY_PRIVATE); builder.setVisibility(android.app.Notification.VISIBILITY_PRIVATE);
builder.setCategory(android.app.Notification.CATEGORY_SOCIAL); builder.setCategory(android.app.Notification.CATEGORY_SOCIAL);

View File

@ -117,6 +117,12 @@ public class NotificationsFragment extends SFragment implements
return rootView; return rootView;
} }
@Override
public void onResume() {
super.onResume();
sendFetchNotificationsRequest();
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout); TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout);

View File

@ -138,6 +138,12 @@ public class TimelineFragment extends SFragment implements
return rootView; return rootView;
} }
@Override
public void onResume() {
super.onResume();
sendFetchTimelineRequest();
}
@Override @Override
public void onDestroyView() { public void onDestroyView() {
if (jumpToTopAllowed()) { if (jumpToTopAllowed()) {

View File

@ -21,20 +21,30 @@ import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import butterknife.BindView;
import butterknife.ButterKnife;
public class ViewTagActivity extends BaseActivity { public class ViewTagActivity extends BaseActivity {
@BindView(R.id.toolbar) Toolbar toolbar;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_tag); setContentView(R.layout.activity_view_tag);
ButterKnife.bind(this);
String hashtag = getIntent().getStringExtra("hashtag"); String hashtag = getIntent().getStringExtra("hashtag");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
ActionBar bar = getSupportActionBar(); ActionBar bar = getSupportActionBar();
if (bar != null) { if (bar != null) {
bar.setTitle(String.format(getString(R.string.title_tag), hashtag)); bar.setTitle(String.format(getString(R.string.title_tag), hashtag));
bar.setDisplayHomeAsUpEnabled(true);
bar.setDisplayShowHomeEnabled(true);
} }
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
@ -42,4 +52,15 @@ public class ViewTagActivity extends BaseActivity {
fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit(); fragmentTransaction.commit();
} }
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
onBackPressed();
return true;
}
}
return super.onOptionsItemSelected(item);
}
} }