Skip to content

Commit

Permalink
[TIMOB-25045] Implement Ti.Android.NotificationChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Mathews committed Aug 21, 2017
1 parent afa65fb commit 8f9f3cd
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import android.app.PendingIntent;
import android.app.Service;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.ActionBar;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -274,6 +275,14 @@ public class AndroidModule extends KrollModule
@Kroll.constant public static final int NAVIGATION_MODE_STANDARD = ActionBar.NAVIGATION_MODE_STANDARD;
@Kroll.constant public static final int NAVIGATION_MODE_TABS = ActionBar.NAVIGATION_MODE_TABS;

@Kroll.constant public static final int IMPORTANCE_DEFAULT = NotificationManagerCompat.IMPORTANCE_DEFAULT;
@Kroll.constant public static final int IMPORTANCE_HIGH = NotificationManagerCompat.IMPORTANCE_HIGH;
@Kroll.constant public static final int IMPORTANCE_LOW = NotificationManagerCompat.IMPORTANCE_LOW;
@Kroll.constant public static final int IMPORTANCE_MAX = NotificationManagerCompat.IMPORTANCE_MAX;
@Kroll.constant public static final int IMPORTANCE_MIN = NotificationManagerCompat.IMPORTANCE_MIN;
@Kroll.constant public static final int IMPORTANCE_NONE = NotificationManagerCompat.IMPORTANCE_NONE;
@Kroll.constant public static final int IMPORTANCE_UNSPECIFIED = NotificationManagerCompat.IMPORTANCE_UNSPECIFIED;

protected RProxy r;
private static final int REQUEST_CODE = 99;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2017 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.android.notificationmanager;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;

import ti.modules.titanium.android.AndroidModule;

import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.os.Build;

@TargetApi(26)
@Kroll.proxy(propertyAccessors = {
TiC.PROPERTY_BYPASS_DND,
TiC.PROPERTY_DESCRIPTION,
TiC.PROPERTY_ENABLE_LIGHTS,
TiC.PROPERTY_ENABLE_VIBRATION,
TiC.PROPERTY_GROUP_ID,
TiC.PROPERTY_IMPORTANCE,
TiC.PROPERTY_LIGHT_COLOR,
TiC.PROPERTY_LOCKSCREEN_VISIBILITY,
TiC.PROPERTY_NAME,
TiC.PROPERTY_SHOW_BADGE,
TiC.PROPERTY_VIBRATE_PATTERN
})
public class NotificationChannelProxy extends KrollProxy
{
private static final String TAG = "TiNotificationChannel";

private NotificationChannel channel = null;

public NotificationChannelProxy()
{
super();
}

@Override
public void handleCreationDict(KrollDict d)
{
super.handleCreationDict(d);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && d != null &&
d.containsKey(TiC.PROPERTY_ID) && d.containsKey(TiC.PROPERTY_NAME) && d.containsKey(TiC.PROPERTY_IMPORTANCE)) {

channel = new NotificationChannel(d.getString(TiC.PROPERTY_ID), d.getString(TiC.PROPERTY_NAME), d.getInt(TiC.PROPERTY_IMPORTANCE));

if (d.containsKey(TiC.PROPERTY_BYPASS_DND)) {
setBypassDnd(d.getBoolean(TiC.PROPERTY_BYPASS_DND));
}
if (d.containsKey(TiC.PROPERTY_DESCRIPTION)) {
setDescription(d.getString(TiC.PROPERTY_DESCRIPTION));
}
if (d.containsKey(TiC.PROPERTY_ENABLE_LIGHTS)) {
setEnableLights(d.getBoolean(TiC.PROPERTY_ENABLE_LIGHTS));
}
if (d.containsKey(TiC.PROPERTY_ENABLE_VIBRATION)) {
setEnableVibration(d.getBoolean(TiC.PROPERTY_ENABLE_VIBRATION));
}
if (d.containsKey(TiC.PROPERTY_GROUP_ID)) {
setGroupId(d.getString(TiC.PROPERTY_GROUP_ID));
}
if (d.containsKey(TiC.PROPERTY_LIGHT_COLOR)) {
setLightColor(d.getInt(TiC.PROPERTY_LIGHT_COLOR));
}
if (d.containsKey(TiC.PROPERTY_LOCKSCREEN_VISIBILITY)) {
setLockscreenVisibility(d.getInt(TiC.PROPERTY_LOCKSCREEN_VISIBILITY));
}
if (d.containsKey(TiC.PROPERTY_SHOW_BADGE)) {
setShowBadge(d.getBoolean(TiC.PROPERTY_SHOW_BADGE));
}
if (d.containsKey(TiC.PROPERTY_VIBRATE_PATTERN)) {
setVibrationPattern((long[]) d.get(TiC.PROPERTY_VIBRATE_PATTERN));
}
} else {
Log.e(TAG, "could not create notification channel");
}
}

@Kroll.method @Kroll.getProperty
public String getId()
{
return channel.getId();
}

@Kroll.method @Kroll.setProperty
public void setEnableLights(boolean lights)
{
channel.enableLights(lights);
setProperty(TiC.PROPERTY_ENABLE_LIGHTS, lights);
}

@Kroll.method @Kroll.setProperty
public void setEnableVibration(boolean vibration)
{
channel.enableVibration(vibration);
setProperty(TiC.PROPERTY_ENABLE_VIBRATION, vibration);
}

@Kroll.method @Kroll.setProperty
public void setBypassDnd(boolean bypassDnd)
{
channel.setBypassDnd(bypassDnd);
setProperty(TiC.PROPERTY_BYPASS_DND, bypassDnd);
}

@Kroll.method @Kroll.setProperty
public void setDescription(String description)
{
channel.setDescription(description);
setProperty(TiC.PROPERTY_DESCRIPTION, description);
}

@Kroll.method @Kroll.setProperty
public void setGroupId(String groupId)
{
channel.setGroup(groupId);
setProperty(TiC.PROPERTY_GROUP_ID, groupId);
}

@Kroll.method @Kroll.setProperty
public void setImportance(int importance)
{
channel.setImportance(importance);
setProperty(TiC.PROPERTY_IMPORTANCE, importance);
}

@Kroll.method @Kroll.setProperty
public void setLightColor(int argb)
{
channel.setLightColor(argb);
setProperty(TiC.PROPERTY_LIGHT_COLOR, argb);
}

@Kroll.method @Kroll.setProperty
public void setLockscreenVisibility(int lockscreenVisibility)
{
channel.setLockscreenVisibility(lockscreenVisibility);
setProperty(TiC.PROPERTY_LOCKSCREEN_VISIBILITY, lockscreenVisibility);
}

@Kroll.method @Kroll.setProperty
public void setName(String name)
{
channel.setName(name);
setProperty(TiC.PROPERTY_NAME, name);
}

@Kroll.method @Kroll.setProperty
public void setShowBadge(boolean showBadge)
{
channel.setShowBadge(showBadge);
setProperty(TiC.PROPERTY_SHOW_BADGE, showBadge);
}

@Kroll.method @Kroll.setProperty
public void setVibrationPattern(long[] vibrationPattern)
{
channel.setVibrationPattern(vibrationPattern);
setProperty(TiC.PROPERTY_VIBRATE_PATTERN, vibrationPattern);
}

public NotificationChannel getNotificationChannel()
{
return channel;
}

@Override
public String getApiName()
{
return "Ti.Android.NotificationChannel";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2016 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2017 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand All @@ -11,9 +11,12 @@
import org.appcelerator.titanium.TiApplication;

import ti.modules.titanium.android.AndroidModule;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Build;

@Kroll.module(parentModule=AndroidModule.class)
public class NotificationManagerModule extends KrollModule
Expand Down Expand Up @@ -55,6 +58,19 @@ private NotificationManager getManager()
return (NotificationManager) TiApplication.getInstance().getSystemService(Activity.NOTIFICATION_SERVICE);
}

@TargetApi(26)
@Kroll.method
public NotificationChannelProxy createNotificationChannel(Object[] args)
{
NotificationChannelProxy notificationChannelProxy = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannelProxy = new NotificationChannelProxy();
notificationChannelProxy.handleCreationArgs(this, args);
getManager().createNotificationChannel(notificationChannelProxy.getNotificationChannel());
}
return notificationChannelProxy;
}

@Kroll.method
public void cancel(int id)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public void handleCreationDict(KrollDict d)
if (d.containsKey(TiC.PROPERTY_AUDIO_STREAM_TYPE)) {
setAudioStreamType(TiConvert.toInt(d, TiC.PROPERTY_AUDIO_STREAM_TYPE));
}
if (d.containsKey(TiC.PROPERTY_CHANNEL_ID)) {
setChannelId(d.getString(TiC.PROPERTY_CHANNEL_ID));
}
if (d.containsKey(TiC.PROPERTY_CONTENT_VIEW)) {
setContentView((RemoteViewsProxy) d.get(TiC.PROPERTY_CONTENT_VIEW));
}
Expand Down Expand Up @@ -364,6 +367,13 @@ public void setLatestEventInfo(String contentTitle, String contentText, PendingI
.setContentText(contentText)
.setContentTitle(contentTitle);
}

@Kroll.method @Kroll.setProperty
public void setChannelId(String channelId)
{
notificationBuilder.setChannelId(channelId);
setProperty(TiC.PROPERTY_CHANNEL_ID, channelId);
}

@Kroll.method
public void setProgress(int max, int progress, boolean indeterminate)
Expand Down
45 changes: 45 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,11 @@ public class TiC
*/
public static final String PROPERTY_BYTE_ORDER = "byteOrder";

/**
* @module.api
*/
public static final String PROPERTY_BYPASS_DND = "bypassDnd";

/**
* @module.api
*/
Expand Down Expand Up @@ -1069,6 +1074,11 @@ public class TiC
*/
public static final String PROPERTY_CHARSET = "charset";

/**
* @module.api
*/
public static final String PROPERTY_CHANNEL_ID = "channelId";

/**
* @module.api
*/
Expand Down Expand Up @@ -1234,6 +1244,11 @@ public class TiC
*/
public static final String PROPERTY_DECODE_RETRIES = "decodeRetries";

/**
* @module.api
*/
public static final String PROPERTY_DESCRIPTION = "description";

/**
* @module.api
*/
Expand Down Expand Up @@ -1334,11 +1349,21 @@ public class TiC
*/
public static final String PROPERTY_ENABLE_JAVASCRIPT_INTERFACE = "enableJavascriptInterface";

/**
* @module.api
*/
public static final String PROPERTY_ENABLE_LIGHTS = "enableLights";

/**
* @module.api
*/
public static final String PROPERTY_ENABLE_RETURN_KEY = "enableReturnKey";

/**
* @module.api
*/
public static final String PROPERTY_ENABLE_VIBRATION = "enableVibrations";

/**
* @module.api
*/
Expand Down Expand Up @@ -1658,6 +1683,11 @@ public class TiC
*/
public static final String PROPERTY_IMAGES = "images";

/**
* @module.api
*/
public static final String PROPERTY_IMPORTANCE = "importance";

/**
* @module.api
*/
Expand Down Expand Up @@ -1763,6 +1793,11 @@ public class TiC
*/
public static final String PROPERTY_LIFECYCLE_CONTAINER = "lifecycleContainer";

/**
* @module.api
*/
public static final String PROPERTY_LIGHT_COLOR = "lightColor";

/**
* @module.api
*/
Expand Down Expand Up @@ -1823,6 +1858,11 @@ public class TiC
*/
public static final String PROPERTY_LOCATION = "location";

/**
* @module.api
*/
public static final String PROPERTY_LOCKSCREEN_VISIBILITY = "lockscreenVisibility";

/**
* @module.api
*/
Expand Down Expand Up @@ -2519,6 +2559,11 @@ public class TiC
*/
public static final String PROPERTY_SHOW_AS_ACTION = "showAsAction";

/**
* @module.api
*/
public static final String PROPERTY_SHOW_BADGE = "showBadge";

/**
* @module.api
*/
Expand Down

0 comments on commit 8f9f3cd

Please sign in to comment.