Skip to content

Commit

Permalink
fix(android): allow largeIcon for BigPicture style to be null (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
helenaford committed Dec 22, 2021
1 parent 2dcdad1 commit 50aa11e
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,18 @@ private Task<NotificationCompat.Style> getBigPictureStyleTask(Executor executor)
}
}

String largeIcon = null;

if (mNotificationAndroidStyleBundle.containsKey("largeIcon")) {
String largeIcon =
Objects.requireNonNull(mNotificationAndroidStyleBundle.getString("largeIcon"));
largeIcon = mNotificationAndroidStyleBundle.getString("largeIcon");

// largeIcon has been specified to be null for BigPicture
if (largeIcon == null) {
bigPictureStyle.bigLargeIcon(null);
}
}

if (largeIcon != null) {
Bitmap largeIconBitmap = null;

try {
Expand Down
3 changes: 3 additions & 0 deletions docs/react-native/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ next: /react-native/docs/usage
previous: /react-native/docs/installation
---

## 4.0.2 (Pending)
- **[Android]**: Add support to set `largeIcon` for `AndroidStyle.BIGPICTURE` to null (Fixes [#270](https://github.com/invertase/notifee/issues/270))

## 4.0.1
- **[Android]**: Fixes an issue with repeating trigger notifications where the next notification was scheduled at the incorrect time, causing the notification to infinitely display (Fixes [#252](https://github.com/invertase/notifee/issues/252)).

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1f0fe4d922daafd64ef528f97beafb7c
e5091b22789f63a7f08246582183e690
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a9cda1115962a578fb6494f042fc077a96ebd7bb
ec6fc392cbdd3d3576bedb3e0d315e7a9ff51cbb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
64484a4d97da7d76da15868903dabdc23fc36d8d3b80f16de4139ddab4244526
ba7b43931221300f28b77695b7f017d52e83d292422fe55e79265ff9f390380b
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d064aa3688238c22989a581b5b42870b2fd168ab44e4acbad0f0a21bdfc218bace4e504cb680ec9c033a28c3d1a2440ea6e910c6fb121f5b098c905c2b40ad2a
242aa6a811d6872694070e38751256b77b7939fb0740abd473ccf703e9b90d4b1556dbb807b3fc503d16efb872c7a10158c13ed8746c51bd3f5f7a3dc4f2e525
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<versions>
<version>202108261754</version>
</versions>
<lastUpdated>20211210003824</lastUpdated>
<lastUpdated>20211221160307</lastUpdated>
</versioning>
</metadata>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3938e27f0c909ea62c38fab526351f1e
49aba3384544c341fae2d93374e738bb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5e732105e5d6d1b0293a2820ae20b9caf708d465
233d2a772b167788329b08eee36ac9b52a8f5ec4
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47c59316e5ffc68aea4c63e5abbb7d3f354fa101c1f1c87446725bba271cf44d
ef5dacb5c7316d43054fb17fce07406a3358e91da2c938053092931d1f37e0e8
Original file line number Diff line number Diff line change
@@ -1 +1 @@
606cfeca5b1d259b08af37d323870b154e1c3b4aaf90d5cdce2784cdb6dec1a5410210d82a419199c5e3fa132a9cf751d94b1aa112e85eaba28d22b037ae8d47
d9bc2563ca878aad65fa2de6fbda232da3920872c00b719f78510dc25e19e63d51d9a200ffbc568f0759c5f234bfd3ea062d942cea3cd349246b3a6911654f65
4 changes: 3 additions & 1 deletion packages/react-native/src/types/NotificationAndroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,9 +546,11 @@ export interface AndroidBigPictureStyle {
* A local file path using the 'require()' method or a HTTP or file URL to the picture to display.
*
* If set, overrides the main notification `largeIcon` when the notification is expanded.
*
* To hide the `largeIcon` when the notification is expanded, set to null. Similar to `thumbnailHidden` for attachments on iOS.
*/
/* eslint-disable-next-line @typescript-eslint/ban-types */
largeIcon?: string | number | object;
largeIcon?: string | number | object | null;

/**
* If set, overrides the main notification `summary` when the notification is expanded.
Expand Down
7 changes: 6 additions & 1 deletion packages/react-native/src/validators/validateAndroidStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ export function validateAndroidBigPictureStyle(
}

if (objectHasProperty(style, 'largeIcon')) {
if (!isString(style.largeIcon) && !isNumber(style.largeIcon) && !isObject(style.largeIcon)) {
if (
style.largeIcon !== null &&
!isString(style.largeIcon) &&
!isNumber(style.largeIcon) &&
!isObject(style.largeIcon)
) {
throw new Error(
"'notification.android.style' BigPictureStyle: 'largeIcon' expected a React Native ImageResource value or a valid string URL.",
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ describe('Validate Android Style', () => {
expect($.summary).toEqual('summary');
});

test('returns valid when largeIcon is null', () => {
const pictureStyle: AndroidBigPictureStyle = {
type: AndroidStyle.BIGPICTURE,
picture: 'picture',
title: 'title',
largeIcon: null,
summary: 'summary',
};

const $ = validateAndroidBigPictureStyle(pictureStyle);
expect($.type).toEqual(AndroidStyle.BIGPICTURE);
expect($.picture).toEqual('picture');
expect($.title).toEqual('title');
expect($.largeIcon).toBeNull();
expect($.summary).toEqual('summary');
});

test('throws an error with an invalid largeIcon param', () => {
const pictureStyle: AndroidBigPictureStyle = {
type: AndroidStyle.BIGPICTURE,
Expand Down
77 changes: 59 additions & 18 deletions tests_react_native/specs/notification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import notifee, {
TimestampTrigger,
TriggerType,
RepeatFrequency,
AndroidStyle,
} from '@notifee/react-native';
import { Platform } from 'react-native';

Expand Down Expand Up @@ -108,25 +109,65 @@ export function NotificationSpec(spec: TestScope): void {
});
});

spec.it('displays a empty notification', async function () {
return new Promise(async (resolve, reject) => {
return notifee
.displayNotification({
title: '',
body: '',
android: {
channelId: 'high',
},
})
.then(id => {
expect(id).equals(id);
resolve();
})
.catch(e => {
reject(e);
spec.it(
'displays a notification with AndroidStyle.BIGPICTURE and largeIcon as null',
async function () {
const testId = 'test-id';
const testLargeIcon = 'test-large-icon';
const testBigPicture = 'test-picture';

if (Platform.OS === 'ios') {
return;
}

return new Promise(async (resolve, reject) => {
const unsubscribe = notifee.onForegroundEvent(async (event: Event) => {
try {
expect(event.type).equals(EventType.DELIVERED);
expect(event.detail.notification?.id).equals(testId);

const androidNotification = event.detail.notification?.android;

expect(androidNotification.style.type).equals(AndroidStyle.BIGPICTURE);

if (androidNotification.style.type === AndroidStyle.BIGPICTURE) {
expect(androidNotification.style.picture).equals(testBigPicture);

expect(androidNotification.style.largeIcon).null;
}

unsubscribe();
resolve();
} catch (e) {
unsubscribe();
reject(e);
}
});
});
});

await notifee
.displayNotification({
id: testId,
title: '',
body: '',
android: {
channelId: 'high',
largeIcon: testLargeIcon,
style: {
type: AndroidStyle.BIGPICTURE,
largeIcon: null,
picture: testBigPicture,
},
},
})
.then(id => {
expect(id).equals(id);
})
.catch(e => {
reject(e);
});
});
},
);

spec.describe('displayNotification with pressAction', function () {
spec.it('displays a notification with a pressAction with id `default`', async function () {
Expand Down

0 comments on commit 50aa11e

Please sign in to comment.