Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

implement 'bigLargeIcon' for Android notifications #1730

Merged
merged 2 commits into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ PushNotification.localNotification({
bigText: "My big text that will be shown when notification is expanded", // (optional) default: "message" prop
subText: "This is a subText", // (optional) default: none
bigPictureUrl: "https://www.example.tld/picture.jpg", // (optional) default: undefined
bigLargeIcon: "ic_launcher", // (optional) default: undefined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this parameter is used...
Can you check it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you're perfectly right, good catch!
I had only tested with url. Fixed it with another commit.

bigLargeIconUrl: "https://www.example.tld/bigicon.jpg", // (optional) default: undefined
color: "red", // (optional) default: system default
vibrate: true, // (optional) default: true
vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,17 @@ public void sendNotificationScheduledCore(Bundle bundle) {

public void sendToNotificationCentre(final Bundle bundle) {
RNPushNotificationPicturesAggregator aggregator = new RNPushNotificationPicturesAggregator(new RNPushNotificationPicturesAggregator.Callback() {
public void call(Bitmap largeIconImage, Bitmap bigPictureImage) {
sendToNotificationCentreWithPicture(bundle, largeIconImage, bigPictureImage);
public void call(Bitmap largeIconImage, Bitmap bigPictureImage, Bitmap bigLargeIconImage) {
sendToNotificationCentreWithPicture(bundle, largeIconImage, bigPictureImage, bigLargeIconImage);
}
});

aggregator.setLargeIconUrl(context, bundle.getString("largeIconUrl"));
aggregator.setBigLargeIconUrl(context, bundle.getString("bigLargeIconUrl"));
aggregator.setBigPictureUrl(context, bundle.getString("bigPictureUrl"));
}

public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconBitmap, Bitmap bigPictureBitmap) {
public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconBitmap, Bitmap bigPictureBitmap, Bitmap bigLargeIconBitmap) {
try {
Class intentClass = getMainActivityClass();
if (intentClass == null) {
Expand Down Expand Up @@ -375,10 +376,26 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB
NotificationCompat.Style style;

if(bigPictureBitmap != null) {

// Big large icon
if(bigLargeIconBitmap == null) {
int bigLargeIconResId = 0;

String bigLargeIcon = bundle.getString("bigLargeIcon");

if (bigLargeIcon != null && !bigLargeIcon.isEmpty()) {
bigLargeIconResId = res.getIdentifier(bigLargeIcon, "mipmap", packageName);
if (bigLargeIconResId != 0) {
bigLargeIconBitmap = BitmapFactory.decodeResource(res, bigLargeIconResId);
}
}
}

style = new NotificationCompat.BigPictureStyle()
.bigPicture(bigPictureBitmap)
.setBigContentTitle(title)
.setSummaryText(message);
.setSummaryText(message)
.bigLargeIcon(bigLargeIconBitmap);
}
else {
style = new NotificationCompat.BigTextStyle().bigText(bigText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@

public class RNPushNotificationPicturesAggregator {
interface Callback {
public void call(Bitmap largeIconImage, Bitmap bigPictureImage);
public void call(Bitmap largeIconImage, Bitmap bigPictureImage, Bitmap bigLargeIconImage);
}

private AtomicInteger count = new AtomicInteger(0);

private Bitmap largeIconImage;
private Bitmap bigPictureImage;
private Bitmap bigLargeIconImage;

private Callback callback;

Expand Down Expand Up @@ -108,6 +109,42 @@ public void onFailureImpl(DataSource dataSource) {
});
}

public void setBigLargeIcon(Bitmap bitmap) {
this.bigLargeIconImage = bitmap;
this.finished();
}

public void setBigLargeIconUrl(Context context, String url) {
if(null == url) {
this.setBigLargeIcon(null);
return;
}

Uri uri = null;

try {
uri = Uri.parse(url);
} catch(Exception ex) {
Log.e(LOG_TAG, "Failed to parse bigLargeIconUrl", ex);
this.setBigLargeIcon(null);
return;
}

final RNPushNotificationPicturesAggregator aggregator = this;

this.downloadRequest(context, uri, new BaseBitmapDataSubscriber() {
@Override
public void onNewResultImpl(@Nullable Bitmap bitmap) {
aggregator.setBigLargeIcon(bitmap);
}

@Override
public void onFailureImpl(DataSource dataSource) {
aggregator.setBigLargeIcon(null);
}
});
}

private void downloadRequest(Context context, Uri uri, BaseBitmapDataSubscriber subscriber) {
ImageRequest imageRequest = ImageRequestBuilder
.newBuilderWithSource(uri)
Expand All @@ -128,8 +165,8 @@ private void finished() {
synchronized(this.count) {
int val = this.count.incrementAndGet();

if(val >= 2 && this.callback != null) {
this.callback.call(this.largeIconImage, this.bigPictureImage);
if(val >= 3 && this.callback != null) {
this.callback.call(this.largeIconImage, this.bigPictureImage, this.bigLargeIconImage);
}
}
}
Expand Down