34
34
import android .content .Context ;
35
35
import android .net .Uri ;
36
36
import android .os .Build ;
37
+
37
38
import androidx .annotation .NonNull ;
38
39
import androidx .annotation .Nullable ;
39
40
import androidx .annotation .RequiresApi ;
44
45
import org .json .JSONObject ;
45
46
46
47
import java .math .BigInteger ;
48
+ import java .util .ArrayList ;
47
49
import java .util .HashSet ;
48
50
import java .util .List ;
49
51
import java .util .Set ;
@@ -58,6 +60,7 @@ class NotificationChannelManager {
58
60
59
61
private static final String DEFAULT_CHANNEL_ID = "fcm_fallback_notification_channel" ;
60
62
private static final String RESTORE_CHANNEL_ID = "restored_OS_notifications" ;
63
+ private static final String CHANNEL_PREFIX = "OS_" ;
61
64
private static final Pattern hexPattern = Pattern .compile ("^([A-Fa-f0-9]{8})$" );
62
65
63
66
static String createNotificationChannel (OSNotificationGenerationJob notificationJob ) {
@@ -216,7 +219,7 @@ static void processChannelList(@NonNull Context context, @Nullable JSONArray lis
216
219
if (Build .VERSION .SDK_INT < Build .VERSION_CODES .O )
217
220
return ;
218
221
219
- if (list == null )
222
+ if (list == null || list . length () == 0 )
220
223
return ;
221
224
222
225
NotificationManager notificationManager = OneSignalNotificationManager .getNotificationManager (context );
@@ -230,13 +233,25 @@ static void processChannelList(@NonNull Context context, @Nullable JSONArray lis
230
233
OneSignal .Log (OneSignal .LOG_LEVEL .ERROR , "Could not create notification channel due to JSON payload error!" , e );
231
234
}
232
235
}
233
-
236
+
237
+ if (syncedChannelSet .isEmpty ())
238
+ return ;
239
+
240
+ List <NotificationChannel > existingChannels = new ArrayList <>();
241
+
242
+ try {
243
+ existingChannels = notificationManager .getNotificationChannels ();
244
+ } catch (NullPointerException e ) {
245
+ // Catch issue caused by "Attempt to invoke virtual method 'boolean android.app.NotificationChannel.isDeleted()' on a null object reference"
246
+ // https://github.com/OneSignal/OneSignal-Android-SDK/issues/1291
247
+ OneSignal .onesignalLog (OneSignal .LOG_LEVEL .ERROR , "Error when trying to delete notification channel: " + e .getMessage ());
248
+ }
249
+
234
250
// Delete old channels - Payload will include all changes for the app. Any extra OS_ ones must
235
- // have been deleted from the dashboard and should be removed.
236
- List <NotificationChannel > existingChannels = notificationManager .getNotificationChannels ();
237
- for (NotificationChannel existingChannel : existingChannels ) {
251
+ // have been deleted from the dashboard and should be removed.
252
+ for (NotificationChannel existingChannel : existingChannels ) {
238
253
String id = existingChannel .getId ();
239
- if (id .startsWith ("OS_" ) && !syncedChannelSet .contains (id ))
254
+ if (id .startsWith (CHANNEL_PREFIX ) && !syncedChannelSet .contains (id ))
240
255
notificationManager .deleteNotificationChannel (id );
241
256
}
242
257
}
0 commit comments