Skip to content

Commit 4ffd23f

Browse files
committed
Add try catch block to getNotificationChannels method
* getNotificationChannels was producing a NullPointerException: NotificationChannel.isDeleted() on a null object reference, this is happeninig on some manufasturer devices * Add try and catch to getNotificationChannels method
1 parent 7f2397e commit 4ffd23f

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

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

Lines changed: 21 additions & 6 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+
3738
import androidx.annotation.NonNull;
3839
import androidx.annotation.Nullable;
3940
import androidx.annotation.RequiresApi;
@@ -44,6 +45,7 @@
4445
import org.json.JSONObject;
4546

4647
import java.math.BigInteger;
48+
import java.util.ArrayList;
4749
import java.util.HashSet;
4850
import java.util.List;
4951
import java.util.Set;
@@ -58,6 +60,7 @@ class NotificationChannelManager {
5860

5961
private static final String DEFAULT_CHANNEL_ID = "fcm_fallback_notification_channel";
6062
private static final String RESTORE_CHANNEL_ID = "restored_OS_notifications";
63+
private static final String CHANNEL_PREFIX = "OS_";
6164
private static final Pattern hexPattern = Pattern.compile("^([A-Fa-f0-9]{8})$");
6265

6366
static String createNotificationChannel(OSNotificationGenerationJob notificationJob) {
@@ -216,7 +219,7 @@ static void processChannelList(@NonNull Context context, @Nullable JSONArray lis
216219
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
217220
return;
218221

219-
if (list == null)
222+
if (list == null || list.length() == 0)
220223
return;
221224

222225
NotificationManager notificationManager = OneSignalNotificationManager.getNotificationManager(context);
@@ -230,13 +233,25 @@ static void processChannelList(@NonNull Context context, @Nullable JSONArray lis
230233
OneSignal.Log(OneSignal.LOG_LEVEL.ERROR, "Could not create notification channel due to JSON payload error!", e);
231234
}
232235
}
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+
234250
// 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) {
238253
String id = existingChannel.getId();
239-
if (id.startsWith("OS_") && !syncedChannelSet.contains(id))
254+
if (id.startsWith(CHANNEL_PREFIX) && !syncedChannelSet.contains(id))
240255
notificationManager.deleteNotificationChannel(id);
241256
}
242257
}

0 commit comments

Comments
 (0)