Commit 3649f4bc by Sebastián Katzer

`image:` accepts remote URLs and local URIs (#76)

parent 14dfd156
......@@ -3,6 +3,7 @@
- [enhancement:] Android 2.x (SDK >= 7) support (Thanks to **khizarsonu**)
- [enhancement:] Scope parameter for `isScheduled` and `getScheduledIds`
- [enhancement:] Callbacks for `cancel` & `cancelAll`
- [enhancement:] `image:` accepts remote URLs and local URIs (Android)
#### Version 0.7.4 (22.03.2014)
- [bugfix:] Platform specific properties were ignored.
......
......@@ -73,6 +73,7 @@ More informations can be found [here][PGB_plugin].
- [enhancement:] Android 2.x (SDK >= 7) support (Thanks to **khizarsonu**)
- [enhancement:] Scope parameter for `isScheduled` and `getScheduledIds`
- [enhancement:] Callbacks for `cancel` & `cancelAll`
- [enhancement:] `image:` accepts remote URLs and local URIs (Android)
#### Further informations
- See [CHANGELOG.md][changelog] to get the full changelog for the plugin.
......@@ -325,18 +326,33 @@ window.plugin.notification.local.setDefaults({ autoCancel: true });
### Small and large icons on Android
By default all notifications will display the app icon. But an specific icon can be defined through the `icon` and `smallIcon` properties.
#### Resource icons
The following example shows how to display the `<package.name>.R.drawable.ic_launcher`icon as the notifications icon.
```javascript
/**
* Displays the <package.name>.R.drawable.ic_launcher icon
*/
window.plugin.notification.local.add({ icon: 'ic_launcher' });
```
/**
* Displays the android.R.drawable.ic_dialog_email icon
*/
See below how to use the `android.R.drawable.ic_dialog_email` icon as the notifications small icon.
```javascript
window.plugin.notification.local.add({ smallIcon: 'ic_dialog_email' });
```
#### Local icons
The `icon` property also accepts local file URIs. The URI points to a relative path within the www folder.
```javascript
window.plugin.notification.local.add({ icon: 'file://img/logo.png' }); //=> /assets/www/img/logo.png
```
#### Remote icons
The `icon` property also accepts remote URLs. If the device cannot download the image, it will fallback to the app icon.
```javascript
window.plugin.notification.local.add({ icon: 'https://cordova.apache.org/images/cordova_bot.png' });
```
### Notification sound on Android
The sound must be a absolute or relative Uri pointing to the sound file. The default sound is `RingtoneManager.TYPE_NOTIFICATION`.
......
......@@ -21,6 +21,10 @@
package de.appplant.cordova.plugin.localnotification;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
......@@ -30,8 +34,14 @@ import org.json.JSONObject;
import android.app.Activity;
import android.app.AlarmManager;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
/**
* Class that helps to store the options that can be specified per alarm.
......@@ -153,21 +163,23 @@ public class Options {
/**
* Returns the icon's ID
*/
public int getIcon () {
int icon = 0;
String iconName = options.optString("icon", "icon");
public Bitmap getIcon () {
String icon = options.optString("icon", "icon");
Bitmap bmp = null;
icon = getIconValue(packageName, iconName);
icon = "https://lh3.ggpht.com/r4Qq0soacA8Lz1gwo81cC5NlsOJE60HCmXOrN7I-pr9dG7ucae83nFY3uBvnFn4G1e5XzcmPRmNUrMIZi-wpSq9e30G9HwQWka8coPc";
if (icon == 0) {
icon = getIconValue("android", iconName);
if (icon.startsWith("http")) {
bmp = getIconFromURL(icon);
} else if (icon.startsWith("file://")) {
bmp = getIconFromURI(icon);
}
if (icon == 0) {
icon = android.R.drawable.ic_menu_info_details;
if (bmp == null) {
bmp = getIconFromRes(icon);
}
return options.optInt("icon", icon);
return bmp;
}
/**
......@@ -184,7 +196,7 @@ public class Options {
}
if (resId == 0) {
resId = getIcon();
resId = getIconValue(packageName, "icon");
}
return options.optInt("smallIcon", resId);
......@@ -249,4 +261,91 @@ public class Options {
return icon;
}
/**
* Converts an resource to Bitmap.
*
* @param icon
* The resource name
* @return
* The corresponding bitmap
*/
private Bitmap getIconFromRes (String icon) {
Resources res = LocalNotification.context.getResources();
int iconId = 0;
iconId = getIconValue(packageName, icon);
if (iconId == 0) {
iconId = getIconValue("android", icon);
}
if (iconId == 0) {
iconId = android.R.drawable.ic_menu_info_details;
}
Bitmap bmp = BitmapFactory.decodeResource(res, iconId);
return bmp;
}
/**
* Converts an Image URL to Bitmap.
*
* @param src
* The external image URL
* @return
* The corresponding bitmap
*/
private Bitmap getIconFromURL (String src) {
Bitmap bmp = null;
ThreadPolicy origMode = StrictMode.getThreadPolicy();
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bmp = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
StrictMode.setThreadPolicy(origMode);
return bmp;
}
/**
* Converts an Image URI to Bitmap.
*
* @param src
* The internal image URI
* @return
* The corresponding bitmap
*/
private Bitmap getIconFromURI (String src) {
AssetManager assets = LocalNotification.context.getAssets();
Bitmap bmp = null;
try {
String path = src.replace("file:/", "www");
InputStream input = assets.open(path);
bmp = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return bmp;
}
}
......@@ -35,8 +35,6 @@ import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
......@@ -118,8 +116,7 @@ public class Receiver extends BroadcastReceiver {
*/
@SuppressLint("NewApi")
private Builder buildNotification () {
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), options.getIcon());
Uri sound = options.getSound();
Uri sound = options.getSound();
Builder notification = new NotificationCompat.Builder(context)
.setContentTitle(options.getTitle())
......@@ -127,7 +124,7 @@ public class Receiver extends BroadcastReceiver {
.setNumber(options.getBadge())
.setTicker(options.getMessage())
.setSmallIcon(options.getSmallIcon())
.setLargeIcon(icon)
.setLargeIcon(options.getIcon())
.setAutoCancel(options.getAutoCancel())
.setOngoing(options.getOngoing());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment