Skip to content
Closed
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
36 changes: 30 additions & 6 deletions Parse/src/main/java/com/parse/NotificationCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -84,20 +87,32 @@ 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) {
style.setSummaryText(staticStyle.mSummaryText);
}
}
}
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();
Expand All @@ -119,6 +134,7 @@ public static class Builder {
CharSequence mContentText;
PendingIntent mContentIntent;
Bitmap mLargeIcon;
int mColor;
int mPriority;
Style mStyle;

Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions Parse/src/main/java/com/parse/ParsePushBroadcastReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package com.parse;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down