Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Android) Feat: Support html attributes in body notifications #668

Merged
merged 2 commits into from
Jan 26, 2022
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 @@ -1324,6 +1324,8 @@ The following Android-specific keys are supported and should be placed inside th
- `notification_android_id` - Identifier used to replace existing notifications in the notification drawer
- If not specified, each request creates a new notification.
- If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.
- `notification_android_body_html` - If is passed, the body of a notification is processed as if it were html, you can use <b>, <i> or <s>
- If not specified, the body of the notification will be processed as plain text.
- `notification_android_icon` - name of a [custom notification icon](#android-custom-notification-icons) in the drawable resources
- if not specified, the plugin will use the default `notification_icon` if it exists; otherwise the default app icon will be displayed
- if a [large icon](#android-large-notification-icon) has been defined, it will also be displayed in the system notification.
Expand Down
47 changes: 35 additions & 12 deletions src/android/FirebasePluginMessagingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.util.Log;
import android.app.Notification;
import android.text.TextUtils;
import android.text.Html;
import android.text.Spanned;
import android.content.ContentResolver;
import android.graphics.Color;
import android.graphics.PorterDuff;
Expand Down Expand Up @@ -117,6 +119,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
String messageType;
String title = null;
String body = null;
String bodyHtml = null;
String id = null;
String sound = null;
String vibrate = null;
Expand Down Expand Up @@ -159,6 +162,7 @@ public void onMessageReceived(RemoteMessage remoteMessage) {
}
if(data.containsKey("notification_title")) title = data.get("notification_title");
if(data.containsKey("notification_body")) body = data.get("notification_body");
if(data.containsKey("notification_android_body_html")) bodyHtml = data.get("notification_android_body_html");
if(data.containsKey("notification_android_channel_id")) channelId = data.get("notification_android_channel_id");
if(data.containsKey("notification_android_id")) id = data.get("notification_android_id");
if(data.containsKey("notification_android_sound")) sound = data.get("notification_android_sound");
Expand Down Expand Up @@ -196,14 +200,14 @@ public void onMessageReceived(RemoteMessage remoteMessage) {

if (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title) || (data != null && !data.isEmpty())) {
boolean showNotification = (FirebasePlugin.inBackground() || !FirebasePlugin.hasNotificationsCallback() || foregroundNotification) && (!TextUtils.isEmpty(body) || !TextUtils.isEmpty(title));
sendMessage(remoteMessage, data, messageType, id, title, body, showNotification, sound, vibrate, light, color, icon, channelId, priority, visibility, image, imageType);
sendMessage(remoteMessage, data, messageType, id, title, body, bodyHtml, showNotification, sound, vibrate, light, color, icon, channelId, priority, visibility, image, imageType);
}
}catch (Exception e){
FirebasePlugin.handleExceptionWithoutContext(e);
}
}

private void sendMessage(RemoteMessage remoteMessage, Map<String, String> data, String messageType, String id, String title, String body, boolean showNotification, String sound, String vibrate, String light, String color, String icon, String channelId, String priority, String visibility, String image, String imageType) {
private void sendMessage(RemoteMessage remoteMessage, Map<String, String> data, String messageType, String id, String title, String body, String bodyHtml, boolean showNotification, String sound, String vibrate, String light, String color, String icon, String channelId, String priority, String visibility, String image, String imageType) {
Log.d(TAG, "sendMessage(): messageType="+messageType+"; showNotification="+showNotification+"; id="+id+"; title="+title+"; body="+body+"; sound="+sound+"; vibrate="+vibrate+"; light="+light+"; color="+color+"; icon="+icon+"; channel="+channelId+"; data="+data.toString());
Bundle bundle = new Bundle();
for (String key : data.keySet()) {
Expand All @@ -213,6 +217,7 @@ private void sendMessage(RemoteMessage remoteMessage, Map<String, String> data,
this.putKVInBundle("id", id, bundle);
this.putKVInBundle("title", title, bundle);
this.putKVInBundle("body", body, bundle);
this.putKVInBundle("body_html", bodyHtml, bundle);
this.putKVInBundle("sound", sound, bundle);
this.putKVInBundle("vibrate", vibrate, bundle);
this.putKVInBundle("light", light, bundle);
Expand Down Expand Up @@ -242,14 +247,25 @@ private void sendMessage(RemoteMessage remoteMessage, Map<String, String> data,
Log.d(TAG, "Channel ID: "+channelId);
}



NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
notificationBuilder
.setContentTitle(title)
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setAutoCancel(true)
.setContentIntent(pendingIntent);

if(bodyHtml != null) {
notificationBuilder
.setContentText(fromHtml(body))
.setStyle(new NotificationCompat.BigTextStyle().bigText(fromHtml(body)));
}else{
notificationBuilder
.setContentText(body)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body));
}


// On Android O+ the sound/lights/vibration are determined by the channel ID
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
// Sound
Expand Down Expand Up @@ -340,15 +356,15 @@ private void sendMessage(RemoteMessage remoteMessage, Map<String, String> data,
if (image != null) {
Log.d(TAG, "Large icon: image="+image);
Bitmap bitmap = getBitmapFromURL(image);

if(imageTypeCircle.equalsIgnoreCase(imageType)) {
bitmap = getCircleBitmap(bitmap);
}
else if(imageTypeBigPicture.equalsIgnoreCase(imageType)) {
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).bigLargeIcon(null));
if(bitmap != null) {
if(imageTypeCircle.equalsIgnoreCase(imageType)) {
bitmap = getCircleBitmap(bitmap);
}
else if(imageTypeBigPicture.equalsIgnoreCase(imageType)) {
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap).bigLargeIcon(null));
}
notificationBuilder.setLargeIcon(bitmap);
}

notificationBuilder.setLargeIcon(bitmap);
}

// Color
Expand Down Expand Up @@ -421,6 +437,13 @@ private Bitmap getCircleBitmap(Bitmap bitmap) {
return output;
}

private Spanned fromHtml(String source) {
if (source != null)
return Html.fromHtml(source);
else
return null;
}

private void putKVInBundle(String k, String v, Bundle b){
if(v != null && !b.containsKey(k)){
b.putString(k, v);
Expand Down