From c8604ad68e7e9cfedb830593ed817e8ac2eeff91 Mon Sep 17 00:00:00 2001 From: S1m Date: Tue, 6 Feb 2024 09:14:31 +0100 Subject: [PATCH] Fix NullPointerException with double push --- .../android/PushNotificationReceiver.java | 4 ++-- .../android/UnifiedPushNotificationReceiver.java | 2 +- .../joinmastodon/android/model/PushNotification.java | 11 ++++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java b/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java index c6c549020..283c7558c 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java +++ b/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java @@ -148,9 +148,9 @@ public class PushNotificationReceiver extends BroadcastReceiver{ } } - public void notifyUnifiedPush(Context context, String accountID, org.joinmastodon.android.model.Notification notification) { + public void notifyUnifiedPush(Context context, AccountSession account, org.joinmastodon.android.model.Notification notification) { // push notifications are only created from the official push notification, so we create a fake from by transforming the notification - PushNotificationReceiver.this.notify(context, PushNotification.fromNotification(context, notification), accountID, notification); + PushNotificationReceiver.this.notify(context, PushNotification.fromNotification(context, account, notification), account.getID(), notification); } private void notify(Context context, PushNotification pn, String accountID, org.joinmastodon.android.model.Notification notification){ diff --git a/mastodon/src/main/java/org/joinmastodon/android/UnifiedPushNotificationReceiver.java b/mastodon/src/main/java/org/joinmastodon/android/UnifiedPushNotificationReceiver.java index 2fc060c0f..4760fd111 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/UnifiedPushNotificationReceiver.java +++ b/mastodon/src/main/java/org/joinmastodon/android/UnifiedPushNotificationReceiver.java @@ -72,7 +72,7 @@ public class UnifiedPushNotificationReceiver extends MessagingReceiver{ result.items .stream() .findFirst() - .ifPresent(value->MastodonAPIController.runInBackground(()->new PushNotificationReceiver().notifyUnifiedPush(context, instance, value))); + .ifPresent(value->MastodonAPIController.runInBackground(()->new PushNotificationReceiver().notifyUnifiedPush(context, account, value))); } @Override diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/PushNotification.java b/mastodon/src/main/java/org/joinmastodon/android/model/PushNotification.java index 1e7ce594e..e483d1652 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/PushNotification.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/PushNotification.java @@ -6,6 +6,7 @@ import com.google.gson.annotations.SerializedName; import org.joinmastodon.android.R; import org.joinmastodon.android.api.RequiredField; +import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.ui.utils.UiUtils; import androidx.annotation.StringRes; @@ -23,7 +24,7 @@ public class PushNotification extends BaseModel{ @RequiredField public String body; - public static PushNotification fromNotification(Context context, Notification notification){ + public static PushNotification fromNotification(Context context, AccountSession account, Notification notification){ PushNotification pushNotification = new PushNotification(); pushNotification.notificationType = switch(notification.type) { case FOLLOW -> PushNotification.Type.FOLLOW; @@ -52,8 +53,12 @@ public class PushNotification extends BaseModel{ }); pushNotification.title = UiUtils.generateFormattedString(notificationTitle, notification.account.displayName).toString(); - pushNotification.icon = notification.status.account.avatarStatic; - pushNotification.body = notification.status.getStrippedText(); + if (notification.status != null) { + pushNotification.icon = notification.status.account.avatarStatic; + pushNotification.body = notification.status.getStrippedText(); + } else { + pushNotification.icon = account.getDefaultAvatarUrl(); + } return pushNotification; }