Skip to content

Commit 646a534

Browse files
committed
Some verifications to avoid Null pointer errors
1 parent cfa89ba commit 646a534

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

android/src/main/java/com/lesimoes/androidnotificationlistener/RNAndroidNotificationListener.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,22 @@ public class RNAndroidNotificationListener extends NotificationListenerService {
1515
@Override
1616
public void onNotificationPosted(StatusBarNotification sbn) {
1717
Notification notification = sbn.getNotification();
18+
19+
if (notification == null || notification.extras == null) return;
20+
1821
String app = sbn.getPackageName();
1922

20-
if (notification.extras.getCharSequence(Notification.EXTRA_TITLE) == null) return;
21-
if (notification.extras.getCharSequence(Notification.EXTRA_TEXT) == null) return;
23+
if (app == null) app = "Unknown";
24+
25+
CharSequence titleChars = notification.extras.getCharSequence(Notification.EXTRA_TITLE)
26+
CharSequence textChars = notification.extras.getCharSequence(Notification.EXTRA_TEXT)
27+
28+
if (titleChars == null || textChars == null) return;
2229

23-
String title = notification.extras.getCharSequence(Notification.EXTRA_TITLE).toString();
24-
String text = notification.extras.getCharSequence(Notification.EXTRA_TEXT).toString();
30+
String title = titleChars.toString();
31+
String text = textChars.toString();
2532

26-
if (text == null) return;
33+
if (text == null || text == "" || title == null || title == "") return;
2734

2835
Log.d(TAG, "Notification received: " + app + " | " + title + " | " + text);
2936

android/src/main/java/com/lesimoes/androidnotificationlistener/RNAndroidNotificationListenerPackage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public List<ViewManager> createViewManagers(ReactApplicationContext reactContext
1717
}
1818

1919
@Override
20-
public List<NativeModule> createNativeModules(
21-
ReactApplicationContext reactContext) {
20+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
2221
List<NativeModule> modules = new ArrayList<>();
2322

2423
modules.add(new RNAndroidNotificationListenerModule(reactContext));

0 commit comments

Comments
 (0)