From 7b191c000a46ab0d137f53a6f7a05ac5c0df0b77 Mon Sep 17 00:00:00 2001
From: Peter Siegmund
Date: Fri, 2 Oct 2015 21:30:33 +0200
Subject: [PATCH] on Lollipop and above it is now possible to set the
background color for the small icon
---
.../java/com/parse/NotificationCompat.java | 36 +++++++++++++++----
.../com/parse/ParsePushBroadcastReceiver.java | 30 ++++++++++++++++
2 files changed, 60 insertions(+), 6 deletions(-)
diff --git a/Parse/src/main/java/com/parse/NotificationCompat.java b/Parse/src/main/java/com/parse/NotificationCompat.java
index 840025cd7..9816e45e2 100644
--- a/Parse/src/main/java/com/parse/NotificationCompat.java
+++ b/Parse/src/main/java/com/parse/NotificationCompat.java
@@ -68,11 +68,14 @@ public Notification build(Builder b) {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
static class NotificationCompatPostJellyBean implements NotificationCompatImpl {
- private Notification.Builder postJellyBeanBuilder;
@Override
public Notification build(Builder b) {
- postJellyBeanBuilder = new Notification.Builder(b.mContext);
- postJellyBeanBuilder.setContentTitle(b.mContentTitle)
+ return createBuilder(b).build();
+ }
+
+ protected Notification.Builder createBuilder(Builder b) {
+ Notification.Builder builder = new Notification.Builder(b.mContext);
+ builder.setContentTitle(b.mContentTitle)
.setContentText(b.mContentText)
.setTicker(b.mNotification.tickerText)
.setSmallIcon(b.mNotification.icon, b.mNotification.iconLevel)
@@ -84,7 +87,7 @@ public Notification build(Builder b) {
if (b.mStyle != null) {
if (b.mStyle instanceof Builder.BigTextStyle) {
Builder.BigTextStyle staticStyle = (Builder.BigTextStyle) b.mStyle;
- Notification.BigTextStyle style = new Notification.BigTextStyle(postJellyBeanBuilder)
+ Notification.BigTextStyle style = new Notification.BigTextStyle(builder)
.setBigContentTitle(staticStyle.mBigContentTitle)
.bigText(staticStyle.mBigText);
if (staticStyle.mSummaryTextSet) {
@@ -92,12 +95,24 @@ public Notification build(Builder b) {
}
}
}
- return postJellyBeanBuilder.build();
+ return builder;
+ }
+ }
+
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP)
+ static class NotificationCompatPostLollipop extends NotificationCompatPostJellyBean {
+ @Override
+ public Notification build(Builder b) {
+ Notification.Builder builder = createBuilder(b);
+ builder.setColor(b.mColor);
+ return builder.build();
}
}
static {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ IMPL = new NotificationCompatPostLollipop();
+ } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
IMPL = new NotificationCompatPostJellyBean();
} else {
IMPL = new NotificationCompatImplBase();
@@ -119,6 +134,7 @@ public static class Builder {
CharSequence mContentText;
PendingIntent mContentIntent;
Bitmap mLargeIcon;
+ int mColor;
int mPriority;
Style mStyle;
@@ -235,6 +251,14 @@ public Builder setLargeIcon(Bitmap icon) {
return this;
}
+ /**
+ * Sets the color behind the notification icon on Lollipop and above.
+ */
+ public Builder setColor(int rgb) {
+ mColor = rgb;
+ return this;
+ }
+
/**
* Setting this flag will make it so the notification is automatically
* canceled when the user clicks it in the panel. The PendingIntent
diff --git a/Parse/src/main/java/com/parse/ParsePushBroadcastReceiver.java b/Parse/src/main/java/com/parse/ParsePushBroadcastReceiver.java
index 4b1dc5295..a48aff7a4 100644
--- a/Parse/src/main/java/com/parse/ParsePushBroadcastReceiver.java
+++ b/Parse/src/main/java/com/parse/ParsePushBroadcastReceiver.java
@@ -8,6 +8,7 @@
*/
package com.parse;
+import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
@@ -94,6 +95,9 @@ public class ParsePushBroadcastReceiver extends BroadcastReceiver {
/** The name of the meta-data field used to override the icon used in Notifications. */
public static final String PROPERTY_PUSH_ICON = "com.parse.push.notification_icon";
+ /** The name of the meta-data field used to override the color used in Notifications. */
+ public static final String PROPERTY_PUSH_COLOR = "com.parse.push.notification_color";
+
protected static final int SMALL_NOTIFICATION_MAX_CHARACTER_LIMIT = 38;
/**
@@ -278,6 +282,31 @@ protected int getSmallIconId(Context context, Intent intent) {
return explicitId != 0 ? explicitId : ManifestInfo.getIconId();
}
+ /**
+ * Retrieves the color to be used in a {@link Notification}. The default implementation uses
+ * the color specified by {@code com.parse.push.notification_color} {@code meta-data} in your
+ * {@code AndroidManifest.xml} with the system fallback color.
+ *
+ * @param context
+ * The {@code Context} in which the receiver is running.
+ * @param intent
+ * An {@code Intent} containing the channel and data of the current push notification.
+ * @return
+ * The color for the {@link Notification}
+ */
+ @TargetApi(Build.VERSION_CODES.LOLLIPOP)
+ protected int getColor(Context context, Intent intent) {
+ Bundle metaData = ManifestInfo.getApplicationMetadata(context);
+ int color = 0;
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+ color = Notification.COLOR_DEFAULT;
+ }
+ if (metaData != null) {
+ color = metaData.getInt(PROPERTY_PUSH_COLOR);
+ }
+ return color != 0 ? context.getResources().getColor(color) : color;
+ }
+
/**
* Retrieves the large icon to be used in a {@link Notification}. This {@code Bitmap} should be
* used to provide special context for a particular {@link Notification}, such as the avatar of
@@ -365,6 +394,7 @@ protected Notification getNotification(Context context, Intent intent) {
.setTicker(tickerText)
.setSmallIcon(this.getSmallIconId(context, intent))
.setLargeIcon(this.getLargeIcon(context, intent))
+ .setColor(this.getColor(context, intent))
.setContentIntent(pContentIntent)
.setDeleteIntent(pDeleteIntent)
.setAutoCancel(true)