Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Issue #1464: Create round bitmap icon for large icon image passed in …
Browse files Browse the repository at this point in the history
…from local resource or url (#1635)

* Adds check for inline && Android version < Android N for forceMainActivityReload(true) in PushHandlerActivity.java

* Revises check for inline && Android version < Android N for forceMainActivityReload(true) in PushHandlerActivity.java

* Revises check for inline && Android version < Android N for forceMainActivityReload(true) in PushHandlerActivity.java

* Revises check for inline && Android version < Android N for forceMainActivityReload(true) in PushHandlerActivity.java

* Revises check for inline && Android version < Android N for forceMainActivityReload(true) in PushHandlerActivity.java

* Adds check for inline && Android version < Android N for onCreate method in PushHandlerActivity.java. If true sets foreground to true allowing click action can be handled in the action callback

* Document foreground default Android M and earlier

* Adds getCircleBitmap method to GCMIntentService.java and shape option to extras for setting notification Large Icon to round

* import PorterDuffXfermode and remove final from declarations in getCircleBitmap method

* Imports remaining Classes required by getCircleBitmap and declares IMG_SHAPE constant

* Imports PorterDuff class and fixes gcmLargeIconShape and gcmLargeIcon variable getString params

* Switches round large image preference condition from a string comparison to a boolean check

* updates ROUND_IMG const to ROUND_IMAGE

* Add Log outputs to print out whether round or square icons are being used

* Makes large icon round by default and eliminates option for square

* Issue #1464: Create round bitmap icon for large icon image passed in from local resource or url
  • Loading branch information
macdonst authored Mar 9, 2017
1 parent a0d51e5 commit 0dd0d46
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
37 changes: 37 additions & 0 deletions docs/PAYLOAD.md
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,43 @@ Produces the following notification.

![2015-07-24 02 17 55](https://cloud.githubusercontent.com/assets/353180/8866900/2df0ab06-3190-11e5-9a81-fdb85bb0f5a4.png)

Finally the Material UI guidelines recommend using a circular icon for the large icon if the subject of the image is a person. This JSON sent from GCM:

```javascript
{
"registration_ids": ["my device id"],
"data": {
"title": "Large Circular Icon",
"message": "Loaded from URL",
"image": "https://pbs.twimg.com/profile_images/837060031895896065/VHIQ4oUf_400x400.jpg",
"image-type": "circle"
}
}
```

Here is an example using node-gcm that sends the above JSON:

```javascript
var gcm = require('node-gcm');
// Replace these with your own values.
var apiKey = "replace with API key";
var deviceID = "my device id";
var service = new gcm.Sender(apiKey);
var message = new gcm.Message();
message.addData('title', 'Large Circular Icon');
message.addData('message', 'Loaded from URL');
message.addData('image', 'https://pbs.twimg.com/profile_images/837060031895896065/VHIQ4oUf_400x400.jpg');
message.addData('image-type', 'circular');
service.send(message, { registrationTokens: [ deviceID ] }, function (err, response) {
if(err) console.error(err);
else console.log(response);
});
```

Produces the following notification.

![screenshot_20170308-214947](https://cloud.githubusercontent.com/assets/353180/23733917/902a4650-0449-11e7-924e-d45a38030c74.png)

## Sound

For Android there are three special values for sound you can use. The first is `default` which will play the phones default notification sound.
Expand Down
47 changes: 44 additions & 3 deletions src/android/com/adobe/phonegap/push/GCMIntentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Paint;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat.WearableExtender;
import android.support.v4.app.RemoteInput;
import android.text.Html;
Expand Down Expand Up @@ -700,19 +705,55 @@ private void setNotificationPriority(Bundle extras, NotificationCompat.Builder m
}
}

private Bitmap getCircleBitmap(Bitmap bitmap) {
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
float cx = bitmap.getWidth()/2;
float cy = bitmap.getHeight()/2;
float radius = cx < cy ? cx : cy;
canvas.drawCircle(cx,cy,radius,paint);

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

bitmap.recycle();

return output;
}

private void setNotificationLargeIcon(Bundle extras, String packageName, Resources resources, NotificationCompat.Builder mBuilder) {
String gcmLargeIcon = extras.getString(IMAGE); // from gcm
String imageType = extras.getString(IMAGE_TYPE, IMAGE_TYPE_SQUARE);
if (gcmLargeIcon != null && !"".equals(gcmLargeIcon)) {
if (gcmLargeIcon.startsWith("http://") || gcmLargeIcon.startsWith("https://")) {
mBuilder.setLargeIcon(getBitmapFromURL(gcmLargeIcon));
Bitmap bitmap = getBitmapFromURL(gcmLargeIcon);
if (IMAGE_TYPE_SQUARE.equalsIgnoreCase(imageType)) {
mBuilder.setLargeIcon(bitmap);
} else {
Bitmap bm = getCircleBitmap(bitmap);
mBuilder.setLargeIcon(bm);
}
Log.d(LOG_TAG, "using remote large-icon from gcm");
} else {
AssetManager assetManager = getAssets();
InputStream istr;
try {
istr = assetManager.open(gcmLargeIcon);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
mBuilder.setLargeIcon(bitmap);
if (IMAGE_TYPE_SQUARE.equalsIgnoreCase(imageType)) {
mBuilder.setLargeIcon(bitmap);
} else {
Bitmap bm = getCircleBitmap(bitmap);
mBuilder.setLargeIcon(bm);
}
Log.d(LOG_TAG, "using assets large-icon from gcm");
} catch (IOException e) {
int largeIconId = 0;
Expand Down
3 changes: 3 additions & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ public interface PushConstants {
public static final String TITLE_KEY = "titleKey";
public static final String NO_CACHE = "no-cache";
public static final String DISMISSED = "dismissed";
public static final String IMAGE_TYPE = "image-type";
public static final String IMAGE_TYPE_SQUARE = "square";
public static final String IMAGE_TYPE_CIRCLE = "circle";
}

0 comments on commit 0dd0d46

Please sign in to comment.