3434import android .content .Context ;
3535import android .net .Uri ;
3636import android .os .Build ;
37+ import android .os .DeadSystemException ;
3738
3839import androidx .annotation .NonNull ;
3940import androidx .annotation .Nullable ;
4748import org .json .JSONObject ;
4849
4950import java .math .BigInteger ;
50- import java .util .ArrayList ;
5151import java .util .HashSet ;
5252import java .util .List ;
5353import 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