Enable/disable push notifications. Clearing notifications

resets the summary of the notifications
This commit is contained in:
Eugen Rochko 2017-03-15 23:45:59 +01:00
parent 47ebcb29bf
commit 3a1181bd82
8 changed files with 86 additions and 27 deletions

View File

@ -55,11 +55,14 @@
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".MyFirebaseMessagingService" android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<receiver android:name=".NotificationClearBroadcastReceiver" />
</application>
</manifest>

View File

@ -28,6 +28,7 @@ import android.text.Spanned;
import android.util.TypedValue;
import android.view.Menu;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
@ -38,6 +39,9 @@ import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
@ -96,6 +100,11 @@ public class BaseActivity extends AppCompatActivity {
return preferences.getString("accessToken", null);
}
protected boolean arePushNotificationsEnabled() {
SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
return preferences.getBoolean("notificationsEnabled", true);
}
protected String getBaseUrl() {
SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
return "https://" + preferences.getString("domain", null);
@ -161,4 +170,32 @@ public class BaseActivity extends AppCompatActivity {
}
return super.onCreateOptionsMenu(menu);
}
protected void enablePushNotifications() {
tuskyAPI.register(getBaseUrl(), getAccessToken(), FirebaseInstanceId.getInstance().getToken()).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
protected void disablePushNotifications() {
tuskyAPI.unregister(getBaseUrl(), getAccessToken()).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
}

View File

@ -39,7 +39,6 @@ import android.widget.TextView;
import com.arlib.floatingsearchview.FloatingSearchView;
import com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter;
import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion;
import com.google.firebase.iid.FirebaseInstanceId;
import com.keylesspalace.tusky.entity.Account;
import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.mikepenz.materialdrawer.AccountHeader;
@ -61,7 +60,6 @@ import java.util.Stack;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
@ -175,17 +173,7 @@ public class MainActivity extends BaseActivity {
}
// Setup push notifications
tuskyAPI.register(getBaseUrl(), getAccessToken(), FirebaseInstanceId.getInstance().getToken()).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d(TAG, "tusky-api reponse: " + response.message());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d(TAG, "tusky-api failure: " + t.getMessage());
}
});
if (arePushNotificationsEnabled()) enablePushNotifications();
}
@Override
@ -277,17 +265,7 @@ public class MainActivity extends BaseActivity {
}
private void logout() {
tuskyAPI.unregister(getBaseUrl(), getAccessToken()).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
if (arePushNotificationsEnabled()) disablePushNotifications();
SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();

View File

@ -55,6 +55,13 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
Log.d(TAG, notificationId);
SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
boolean enabled = preferences.getBoolean("notificationsEnabled", true);
if (!enabled) {
return;
}
createMastodonAPI();
mastodonAPI.notification(notificationId).enqueue(new Callback<Notification>() {
@ -152,9 +159,13 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService {
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Intent deleteIntent = new Intent(this, NotificationClearBroadcastReceiver.class);
PendingIntent deletePendingIntent = PendingIntent.getBroadcast(this, 0, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notify)
.setContentIntent(resultPendingIntent)
.setDeleteIntent(deletePendingIntent)
.setDefaults(0); // So it doesn't ring twice, notify only in Target callback
if (currentNotifications.length() == 1) {

View File

@ -0,0 +1,16 @@
package com.keylesspalace.tusky;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
public class NotificationClearBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
SharedPreferences notificationPreferences = context.getSharedPreferences("Notifications", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = notificationPreferences.edit();
editor.putString("current", "[]");
editor.apply();
}
}

View File

@ -68,6 +68,14 @@ public class PreferencesActivity extends BaseActivity
startActivity(intent);
finish();
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
} else if (key.equals("notificationsEnabled")) {
boolean notificationsEnabled = sharedPreferences.getBoolean("notificationsEnabled", true);
if (notificationsEnabled) {
enablePushNotifications();
} else {
disablePushNotifications();
}
}
}

View File

@ -133,4 +133,5 @@
<string name="notification_summary_medium">%1$s, %2$s, and %3$s</string>
<string name="notification_summary_small">%1$s and %2$s</string>
<string name="notification_title_summary">%d new interactions</string>
<string name="pref_title_notifications_enabled">Push notifications</string>
</resources>

View File

@ -1,23 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="@string/preferences_file_key">
<PreferenceCategory android:title="@string/pref_title_notification_settings">
<CheckBoxPreference
android:key="notificationsEnabled"
android:title="@string/pref_title_notifications_enabled"
android:defaultValue="true" />
<CheckBoxPreference
android:dependency="notificationsEnabled"
android:key="notificationAlertSound"
android:title="@string/pref_title_notification_alert_sound"
android:defaultValue="true" />
<CheckBoxPreference
android:dependency="notificationsEnabled"
android:key="notificationStyleVibrate"
android:title="@string/pref_title_notification_style_vibrate"
android:defaultValue="true" />
<CheckBoxPreference
android:dependency="notificationsEnabled"
android:key="notificationStyleLight"
android:title="@string/pref_title_notification_style_light"
android:defaultValue="true" />
</PreferenceCategory>
</PreferenceScreen>