34
34
import android .content .Context ;
35
35
import android .net .Uri ;
36
36
import android .os .Build ;
37
+ import android .os .DeadSystemException ;
37
38
38
39
import androidx .annotation .NonNull ;
39
40
import androidx .annotation .Nullable ;
47
48
import org .json .JSONObject ;
48
49
49
50
import java .math .BigInteger ;
50
- import java .util .ArrayList ;
51
51
import java .util .HashSet ;
52
52
import java .util .List ;
53
53
import java .util .Set ;
@@ -239,14 +239,9 @@ static void processChannelList(@NonNull Context context, @Nullable JSONArray lis
239
239
if (syncedChannelSet .isEmpty ())
240
240
return ;
241
241
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 ;
250
245
}
251
246
252
247
// 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
257
252
notificationManager .deleteNotificationChannel (id );
258
253
}
259
254
}
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
+
261
278
private static int priorityToImportance (int priority ) {
262
279
if (priority > 9 )
263
280
return NotificationManagerCompat .IMPORTANCE_MAX ;
0 commit comments