From daf7e6ed6a6a4eb075ab88aadc6ea408e1b8b2d9 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 12 Mar 2017 08:49:38 +0100 Subject: [PATCH] Prevent double notifications Check timelines for updates on resume --- .../tusky/MyFirebaseMessagingService.java | 30 ++++++++++--------- .../tusky/NotificationsFragment.java | 6 ++++ .../keylesspalace/tusky/TimelineFragment.java | 6 ++++ .../keylesspalace/tusky/ViewTagActivity.java | 23 +++++++++++++- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java b/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java index 20b5cd585..31060869e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java +++ b/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java @@ -109,7 +109,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { } private void buildNotification(Notification body) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); + final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); Intent resultIntent = new Intent(this, MainActivity.class); resultIntent.putExtra("tab_position", 1); @@ -121,7 +121,8 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notify) .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); @@ -129,6 +130,19 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 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()); } @@ -148,18 +162,6 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { .placeholder(R.drawable.avatar_default) .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) { builder.setVisibility(android.app.Notification.VISIBILITY_PRIVATE); builder.setCategory(android.app.Notification.CATEGORY_SOCIAL); diff --git a/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java index a06441d6b..60798f953 100644 --- a/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/NotificationsFragment.java @@ -117,6 +117,12 @@ public class NotificationsFragment extends SFragment implements return rootView; } + @Override + public void onResume() { + super.onResume(); + sendFetchNotificationsRequest(); + } + @Override public void onDestroyView() { TabLayout tabLayout = (TabLayout) getActivity().findViewById(R.id.tab_layout); diff --git a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java index b8c0bfbc2..76579307a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/TimelineFragment.java @@ -138,6 +138,12 @@ public class TimelineFragment extends SFragment implements return rootView; } + @Override + public void onResume() { + super.onResume(); + sendFetchTimelineRequest(); + } + @Override public void onDestroyView() { if (jumpToTopAllowed()) { diff --git a/app/src/main/java/com/keylesspalace/tusky/ViewTagActivity.java b/app/src/main/java/com/keylesspalace/tusky/ViewTagActivity.java index 39dcef2fc..84354d212 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ViewTagActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ViewTagActivity.java @@ -21,20 +21,30 @@ import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; import android.support.v7.widget.Toolbar; +import android.view.MenuItem; + +import butterknife.BindView; +import butterknife.ButterKnife; public class ViewTagActivity extends BaseActivity { + @BindView(R.id.toolbar) Toolbar toolbar; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_tag); + ButterKnife.bind(this); + String hashtag = getIntent().getStringExtra("hashtag"); - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); ActionBar bar = getSupportActionBar(); + if (bar != null) { bar.setTitle(String.format(getString(R.string.title_tag), hashtag)); + bar.setDisplayHomeAsUpEnabled(true); + bar.setDisplayShowHomeEnabled(true); } FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); @@ -42,4 +52,15 @@ public class ViewTagActivity extends BaseActivity { fragmentTransaction.add(R.id.fragment_container, fragment); fragmentTransaction.commit(); } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: { + onBackPressed(); + return true; + } + } + return super.onOptionsItemSelected(item); + } }