Skip to content

Commit dd73d5b

Browse files
committed
DeadSystemException on getNotificationChannels
Handle DeadSystemException that can be thrown from getNotificationChannels
1 parent c18e988 commit dd73d5b

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/NotificationChannelManager.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import android.content.Context;
3535
import android.net.Uri;
3636
import android.os.Build;
37+
import android.os.DeadSystemException;
3738

3839
import androidx.annotation.NonNull;
3940
import androidx.annotation.Nullable;
@@ -47,7 +48,6 @@
4748
import org.json.JSONObject;
4849

4950
import java.math.BigInteger;
50-
import java.util.ArrayList;
5151
import java.util.HashSet;
5252
import java.util.List;
5353
import java.util.Set;
@@ -239,14 +239,9 @@ static void processChannelList(@NonNull Context context, @Nullable JSONArray lis
239239
if (syncedChannelSet.isEmpty())
240240
return;
241241

242-
List<NotificationChannel> existingChannels = new ArrayList<>();
243-
244-
try {
245-
existingChannels = notificationManager.getNotificationChannels();
246-
} catch (NullPointerException e) {
247-
// Catch issue caused by "Attempt to invoke virtual method 'boolean android.app.NotificationChannel.isDeleted()' on a null object reference"
248-
// https://github.com/OneSignal/OneSignal-Android-SDK/issues/1291
249-
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "Error when trying to delete notification channel: " + e.getMessage());
242+
List<NotificationChannel> existingChannels = getChannelList(notificationManager);
243+
if (existingChannels == null) {
244+
return;
250245
}
251246

252247
// Delete old channels - Payload will include all changes for the app. Any extra OS_ ones must
@@ -257,7 +252,29 @@ static void processChannelList(@NonNull Context context, @Nullable JSONArray lis
257252
notificationManager.deleteNotificationChannel(id);
258253
}
259254
}
260-
255+
256+
@RequiresApi(api = Build.VERSION_CODES.O)
257+
private static List<NotificationChannel> getChannelList(NotificationManager notificationManager) {
258+
try {
259+
return notificationManager.getNotificationChannels();
260+
} catch (NullPointerException e) {
261+
// Catching known Android bug, Sometimes throws,
262+
// "Attempt to invoke virtual method 'boolean android.app.NotificationChannel.isDeleted()' on a null object reference"
263+
// https://github.com/OneSignal/OneSignal-Android-SDK/issues/1291
264+
OneSignal.onesignalLog(OneSignal.LOG_LEVEL.ERROR, "Error when trying to delete notification channel: " + e.getMessage());
265+
}
266+
catch (Exception e) {
267+
// Suppressing DeadSystemException as the app is already dying for
268+
// another reason and allowing this exception to bubble up would
269+
// create a red herring for app developers. We still re-throw
270+
// others, as we don't want to silently hide other issues.
271+
if (!(e instanceof DeadSystemException)) {
272+
throw e;
273+
}
274+
}
275+
return null;
276+
}
277+
261278
private static int priorityToImportance(int priority) {
262279
if (priority > 9)
263280
return NotificationManagerCompat.IMPORTANCE_MAX;

0 commit comments

Comments
 (0)