|
|
@ -22,6 +22,9 @@ import com.keylesspalace.tusky.entity.Notification; |
|
|
|
import com.squareup.picasso.Picasso; |
|
|
|
import com.squareup.picasso.Picasso; |
|
|
|
import com.squareup.picasso.Target; |
|
|
|
import com.squareup.picasso.Target; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.json.JSONArray; |
|
|
|
|
|
|
|
import org.json.JSONException; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
|
|
|
import okhttp3.Interceptor; |
|
|
|
import okhttp3.Interceptor; |
|
|
@ -36,6 +39,7 @@ import retrofit2.converter.gson.GsonConverterFactory; |
|
|
|
public class MyFirebaseMessagingService extends FirebaseMessagingService { |
|
|
|
public class MyFirebaseMessagingService extends FirebaseMessagingService { |
|
|
|
private MastodonAPI mastodonAPI; |
|
|
|
private MastodonAPI mastodonAPI; |
|
|
|
private static final String TAG = "MyFirebaseMessagingService"; |
|
|
|
private static final String TAG = "MyFirebaseMessagingService"; |
|
|
|
|
|
|
|
public static final int NOTIFY_ID = 666; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void onMessageReceived(RemoteMessage remoteMessage) { |
|
|
|
public void onMessageReceived(RemoteMessage remoteMessage) { |
|
|
@ -112,6 +116,34 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { |
|
|
|
|
|
|
|
|
|
|
|
private void buildNotification(Notification body) { |
|
|
|
private void buildNotification(Notification body) { |
|
|
|
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); |
|
|
|
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); |
|
|
|
|
|
|
|
final SharedPreferences notificationPreferences = getApplicationContext().getSharedPreferences("Notifications", MODE_PRIVATE); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String rawCurrentNotifications = notificationPreferences.getString("current", "[]"); |
|
|
|
|
|
|
|
JSONArray currentNotifications; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
currentNotifications = new JSONArray(rawCurrentNotifications); |
|
|
|
|
|
|
|
} catch (JSONException e) { |
|
|
|
|
|
|
|
currentNotifications = new JSONArray(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean alreadyContains = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < currentNotifications.length(); i++) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (currentNotifications.getString(i).equals(body.account.displayName)) { |
|
|
|
|
|
|
|
alreadyContains = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (JSONException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!alreadyContains) currentNotifications.put(body.account.displayName); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SharedPreferences.Editor editor = notificationPreferences.edit(); |
|
|
|
|
|
|
|
editor.putString("current", currentNotifications.toString()); |
|
|
|
|
|
|
|
editor.commit(); |
|
|
|
|
|
|
|
|
|
|
|
Intent resultIntent = new Intent(this, MainActivity.class); |
|
|
|
Intent resultIntent = new Intent(this, MainActivity.class); |
|
|
|
resultIntent.putExtra("tab_position", 1); |
|
|
|
resultIntent.putExtra("tab_position", 1); |
|
|
@ -122,11 +154,12 @@ 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) |
|
|
|
|
|
|
|
.setContentIntent(resultPendingIntent) |
|
|
|
.setContentIntent(resultPendingIntent) |
|
|
|
.setDefaults(0); // So it doesn't ring twice, notify only in Target callback
|
|
|
|
.setDefaults(0); // So it doesn't ring twice, notify only in Target callback
|
|
|
|
|
|
|
|
|
|
|
|
final Integer mId = (int)(System.currentTimeMillis() / 1000); |
|
|
|
if (currentNotifications.length() == 1) { |
|
|
|
|
|
|
|
builder.setContentTitle(titleForType(body)) |
|
|
|
|
|
|
|
.setContentText(truncateWithEllipses(bodyForType(body), 40)); |
|
|
|
|
|
|
|
|
|
|
|
Target mTarget = new Target() { |
|
|
|
Target mTarget = new Target() { |
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -145,7 +178,7 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { |
|
|
|
builder.setLights(0xFF00FF8F, 300, 1000); |
|
|
|
builder.setLights(0xFF00FF8F, 300, 1000); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
((NotificationManager) (getSystemService(NOTIFICATION_SERVICE))).notify(mId, builder.build()); |
|
|
|
((NotificationManager) (getSystemService(NOTIFICATION_SERVICE))).notify(NOTIFY_ID, builder.build()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -164,31 +197,60 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { |
|
|
|
.placeholder(R.drawable.avatar_default) |
|
|
|
.placeholder(R.drawable.avatar_default) |
|
|
|
.transform(new RoundedTransformation(7, 0)) |
|
|
|
.transform(new RoundedTransformation(7, 0)) |
|
|
|
.into(mTarget); |
|
|
|
.into(mTarget); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
builder.setContentTitle(String.format(getString(R.string.notification_title_summary), currentNotifications.length())) |
|
|
|
|
|
|
|
.setContentText(truncateWithEllipses(joinNames(currentNotifications), 40)); |
|
|
|
|
|
|
|
} catch (JSONException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch (body.type) { |
|
|
|
((NotificationManager) (getSystemService(NOTIFICATION_SERVICE))).notify(NOTIFY_ID, builder.build()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String joinNames(JSONArray array) throws JSONException { |
|
|
|
|
|
|
|
if (array.length() > 3) { |
|
|
|
|
|
|
|
return String.format(getString(R.string.notification_summary_large), array.get(0), array.get(1), array.get(2), array.length() - 3); |
|
|
|
|
|
|
|
} else if (array.length() == 3) { |
|
|
|
|
|
|
|
return String.format(getString(R.string.notification_summary_medium), array.get(0), array.get(1), array.get(2)); |
|
|
|
|
|
|
|
} else if (array.length() == 2) { |
|
|
|
|
|
|
|
return String.format(getString(R.string.notification_summary_small), array.get(0), array.get(1)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String titleForType(Notification notification) { |
|
|
|
|
|
|
|
switch (notification.type) { |
|
|
|
case MENTION: |
|
|
|
case MENTION: |
|
|
|
builder.setContentTitle(String.format(getString(R.string.notification_mention_format), body.account.getDisplayName())) |
|
|
|
return String.format(getString(R.string.notification_mention_format), notification.account.getDisplayName()); |
|
|
|
.setContentText(truncateWithEllipses(body.status.content.toString(), 40)); |
|
|
|
case FOLLOW: |
|
|
|
break; |
|
|
|
return String.format(getString(R.string.notification_follow_format), notification.account.getDisplayName()); |
|
|
|
|
|
|
|
case FAVOURITE: |
|
|
|
|
|
|
|
return String.format(getString(R.string.notification_favourite_format), notification.account.getDisplayName()); |
|
|
|
|
|
|
|
case REBLOG: |
|
|
|
|
|
|
|
return String.format(getString(R.string.notification_reblog_format), notification.account.getDisplayName()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String bodyForType(Notification notification) { |
|
|
|
|
|
|
|
switch (notification.type) { |
|
|
|
case FOLLOW: |
|
|
|
case FOLLOW: |
|
|
|
builder.setContentTitle(String.format(getString(R.string.notification_follow_format), body.account.getDisplayName())) |
|
|
|
return notification.account.username; |
|
|
|
.setContentText(truncateWithEllipses(body.account.username, 40)); |
|
|
|
case MENTION: |
|
|
|
break; |
|
|
|
|
|
|
|
case FAVOURITE: |
|
|
|
case FAVOURITE: |
|
|
|
builder.setContentTitle(String.format(getString(R.string.notification_favourite_format), body.account.getDisplayName())) |
|
|
|
|
|
|
|
.setContentText(truncateWithEllipses(body.status.content.toString(), 40)); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case REBLOG: |
|
|
|
case REBLOG: |
|
|
|
builder.setContentTitle(String.format(getString(R.string.notification_reblog_format), body.account.getDisplayName())) |
|
|
|
return notification.status.content.toString(); |
|
|
|
.setContentText(truncateWithEllipses(body.status.content.toString(), 40)); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
((NotificationManager) (getSystemService(NOTIFICATION_SERVICE))).notify(mId, builder.build()); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|