Commit 9a7bdb4b by Rahul Kumar Committed by Sebastián Katzer

0.8 Android O support (#1441)

* Support for android version >= O

added option to provide params to create notification channel which is necessary on android version >= O

* Creating notification channel for build>=android O

if device build>=android O, creating a notification channel before notificationManager.build() is called

* using notification builder's new constructor

`NotificationCompat.Builder (Context context)`  was deprecated in API 26.1.0
Using new  `NotificationCompat.Builder(Context, String)` instead.
The second parameter here is notification channel id on which notifiction will be posted.

* Update Notification.java

* For API 26+ : added channel params in options

Added channelParams object inside default options to support passing parameters for notification channel
e.g. a notification object can be made like this:
`notif = { channelParams : { channelID : "channel_id", channelName : "channel_name", description : "channel_description", importance : "notification_importance"}, text: "notif_message", title: "notif_title", sound: "sound_uri"}`

Channels are a way of categorizing your notifications and is necessary to implement on android version >= Android O in order to use notifications. For more info on NotificationChannels you can visit:  https://developer.android.com/reference/android/app/NotificationChannel.html

* Moved channelParams to platform specific options
parent ee7432f4
......@@ -121,7 +121,7 @@ public class Builder {
long[] vibrate = options.getVibrate();
NotificationCompat.Builder builder;
builder = new NotificationCompat.Builder(context)
builder = new NotificationCompat.Builder(context, options.getChannelID())
.setDefaults(0)
.setContentTitle(options.getTitle())
.setContentText(options.getText())
......@@ -129,7 +129,8 @@ public class Builder {
.setTicker(options.getText())
.setAutoCancel(options.isAutoClear())
.setOngoing(options.isOngoing())
.setColor(options.getColor());
.setColor(options.getColor())
.setChannelId(options.getChannelID());
if (ledColor != 0) {
builder.setLights(ledColor, options.getLedOnTime(), options.getLedOffTime());
......
......@@ -25,6 +25,7 @@ package de.appplant.cordova.plugin.notification;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
......@@ -32,6 +33,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.annotation.RequiresApi;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -228,18 +230,40 @@ public class Notification {
* Show as local notification when in background.
*/
@SuppressWarnings("deprecation")
private void showNotification () {
private void showNotification() {
int id = getOptions().getId();
NotificationManager mgr = getNotMgr();
/**
* create a channel before notify for API >= Android O
*/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
createChannel(mgr);
if (Build.VERSION.SDK_INT <= 15) {
// Notification for HoneyComb to ICS
getNotMgr().notify(id, builder.getNotification());
mgr.notify(id, builder.getNotification());
} else {
// Notification for Jellybean and above
getNotMgr().notify(id, builder.build());
mgr.notify(id, builder.build());
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannel(NotificationManager mgr) {
NotificationChannel mChannel = new NotificationChannel(
getOptions().getChannelID(),
getOptions().getChannelName(),
getOptions().getImportance());
mChannel.setDescription(getOptions().getChannelDescription());
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(getOptions().getLedColor());
mChannel.enableVibration(true);
mChannel.setVibrationPattern(getOptions().getVibrate());
mgr.createNotificationChannel(mChannel);
}
/**
* Count of triggers since schedule.
*/
......
......@@ -24,9 +24,11 @@
package de.appplant.cordova.plugin.notification;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import org.json.JSONException;
......@@ -57,6 +59,11 @@ public class Options {
// Asset util instance
private final AssetUtil assets;
//notification channel options for API>=O
private String channelID = "default";
private String channelName = "Miscellaneous";
private String channelDescription = "";
private int importance = NotificationManager.IMPORTANCE_DEFAULT;
/**
* Constructor
......@@ -75,16 +82,46 @@ public class Options {
* @param options
* JSON properties
*/
public Options parse (JSONObject options) {
public Options parse(JSONObject options) {
this.options = options;
parseInterval();
parseAssets();
//parse notification channel params if android version>=O
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
parseChannelParams();
return this;
}
/**
* parse notification channel params
* for api>=android O
*/
private void parseChannelParams() {
JSONObject channelOptions = options.optJSONObject("channelParams");
if (channelOptions != null) {
String id = channelOptions.optString("channelID");
String name = channelOptions.optString("channelName");
String description = channelOptions.optString("description");
int imp = channelOptions.optInt("importance");
if (!id.isEmpty()) {
channelID = id;
}
if (!name.isEmpty()) {
channelName = name;
}
if (!description.isEmpty()) {
channelDescription = description;
}
if (imp >= NotificationManager.IMPORTANCE_NONE
&& imp < NotificationManager.IMPORTANCE_MAX) {
importance = imp;
}
}
}
/**
* Parse repeat interval.
*/
private void parseInterval() {
......@@ -380,6 +417,34 @@ public class Options {
}
/**
* get channel ID
*/
public String getChannelID() {
return channelID;
}
/**
* get channel name
*/
public String getChannelName() {
return channelName;
}
/**
* get channel description
*/
public String getChannelDescription() {
return channelDescription;
}
/**
* get importance
*/
public int getImportance() {
return importance;
}
/**
* JSON object as string.
*/
public String toString() {
......
......@@ -72,6 +72,7 @@ exports.applyPlatformSpecificOptions = function () {
defaults.ledOffTime = undefined;
defaults.color = undefined;
defaults.vibrate = undefined;
defaults.channelParams = {};
break;
}
......@@ -93,6 +94,7 @@ exports.mergeWithDefaults = function (options) {
options.at = this.getValueFor(options, 'at', 'firstAt', 'date');
options.text = this.getValueFor(options, 'text', 'message');
options.data = this.getValueFor(options, 'data', 'json');
options.channelParams = this.getValueFor(options, 'channelParams');
if (defaults.hasOwnProperty('autoClear')) {
options.autoClear = this.getValueFor(options, 'autoClear', 'autoCancel');
......
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