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

Feature/added support for notification color #266

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ window.geofence.addOrUpdate({
text: String, //Text of notification
smallIcon: String, //Small icon showed in notification area, only res URI
icon: String, //icon showed in notification drawer
color: String, //Notification color, only hex
openAppOnClick: Boolean,//is main app activity should be opened after clicking on notification
vibration: [Integer], //Optional vibration pattern - see description
data: Object //Custom object associated with notification
Expand Down
1 change: 1 addition & 0 deletions src/android/GeoNotificationNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void notify(Notification notification) {
notification.setContext(context);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setVibrate(notification.getVibrate())
.setColor(notification.getColor())
.setSmallIcon(notification.getSmallIcon())
.setLargeIcon(notification.getLargeIcon())
.setAutoCancel(true)
Expand Down
37 changes: 36 additions & 1 deletion src/android/Notification.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

import com.google.gson.annotations.Expose;

Expand All @@ -16,6 +18,7 @@ public class Notification {
@Expose public long[] vibrate = new long[] { 1000 };
@Expose public String icon = "";
@Expose public String smallIcon = "";
@Expose public String color;
@Expose public Object data;
@Expose public boolean openAppOnClick;

Expand Down Expand Up @@ -49,12 +52,40 @@ public Bitmap getLargeIcon() {
Uri uri = assets.parse(this.icon);
bmp = assets.getIconFromUri(uri);
} catch (Exception e){
bmp = assets.getIconFromDrawable(this.icon);
bmp = null;
}

return bmp;
}

public int getColor() {
String hex = this.color;

if (hex == null)
return NotificationCompat.COLOR_DEFAULT;

try {
hex = stripHex(hex);

if (hex.matches("[^0-9]*")) {
return Color.class
.getDeclaredField(hex.toUpperCase())
.getInt(null);
}

int aRGB = Integer.parseInt(hex, 16);
return aRGB + 0xFF000000;
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

return NotificationCompat.COLOR_DEFAULT;
}

public String getDataJson() {
if (this.data == null) {
return "";
Expand All @@ -77,4 +108,8 @@ private long[] concat(long[] a, long[] b) {
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}

private String stripHex(String hex) {
return (hex.charAt(0) == '#') ? hex.substring(1) : hex;
}
}