Example usage for android.support.v4.app NotificationCompat.Builder getClass

List of usage examples for android.support.v4.app NotificationCompat.Builder getClass

Introduction

In this page you can find the example usage for android.support.v4.app NotificationCompat.Builder getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * Sets the notification channel on a notification builder.  Uses service properties to 
 * set properties of channel.//  w  ww . j  a  v a  2 s .c om
 * @param nm The notification manager.
 * @param mNotifyBuilder The notify builder
 * @param context The context
 */
public static void setNotificationChannel(NotificationManager nm, NotificationCompat.Builder mNotifyBuilder,
        Context context) {
    if (android.os.Build.VERSION.SDK_INT >= 26) {
        try {
            NotificationManager mNotificationManager = nm;

            String id = getServiceProperty("android.NotificationChannel.id", "cn1-channel", context);

            CharSequence name = getServiceProperty("android.NotificationChannel.name", "Notifications",
                    context);

            String description = getServiceProperty("android.NotificationChannel.description",
                    "Remote notifications", context);

            // NotificationManager.IMPORTANCE_LOW = 2
            // NotificationManager.IMPORTANCE_HIGH = 4  // <-- Minimum level to produce sound.
            int importance = Integer
                    .parseInt(getServiceProperty("android.NotificationChannel.importance", "4", context));
            // Note: Currently we use a single notification channel for the app, but if the app uses different kinds of 
            // push notifications, then this may not be sufficient.   E.g. The app may send both silent push notifications
            // and regular notifications - but their settings (e.g. sound) are all managed through one channel with
            // same settings. 
            // TODO Add support for multiple channels.
            // See https://github.com/codenameone/CodenameOne/issues/2583

            Class clsNotificationChannel = Class.forName("android.app.NotificationChannel");
            //android.app.NotificationChannel mChannel = new android.app.NotificationChannel(id, name, importance);
            Constructor constructor = clsNotificationChannel.getConstructor(java.lang.String.class,
                    java.lang.CharSequence.class, int.class);
            Object mChannel = constructor.newInstance(new Object[] { id, name, importance });

            Method method = clsNotificationChannel.getMethod("setDescription", java.lang.String.class);
            method.invoke(mChannel, new Object[] { description });
            //mChannel.setDescription(description);

            method = clsNotificationChannel.getMethod("enableLights", boolean.class);
            method.invoke(mChannel, new Object[] { Boolean.parseBoolean(
                    getServiceProperty("android.NotificationChannel.enableLights", "true", context)) });
            //mChannel.enableLights(Boolean.parseBoolean(getServiceProperty("android.NotificationChannel.enableLights", "true", context)));

            method = clsNotificationChannel.getMethod("setLightColor", int.class);
            method.invoke(mChannel,
                    new Object[] { Integer.parseInt(getServiceProperty("android.NotificationChannel.lightColor",
                            "" + android.graphics.Color.RED, context)) });
            //mChannel.setLightColor(Integer.parseInt(getServiceProperty("android.NotificationChannel.lightColor", "" + android.graphics.Color.RED, context)));

            method = clsNotificationChannel.getMethod("enableVibration", boolean.class);
            method.invoke(mChannel, new Object[] { Boolean.parseBoolean(
                    getServiceProperty("android.NotificationChannel.enableVibration", "false", context)) });
            //mChannel.enableVibration(Boolean.parseBoolean(getServiceProperty("android.NotificationChannel.enableVibration", "false", context)));
            String vibrationPatternStr = getServiceProperty("android.NotificationChannel.vibrationPattern",
                    null, context);
            if (vibrationPatternStr != null) {
                String[] parts = vibrationPatternStr.split(",");
                int len = parts.length;
                long[] pattern = new long[len];
                for (int i = 0; i < len; i++) {
                    pattern[i] = Long.parseLong(parts[i].trim());
                }
                method = clsNotificationChannel.getMethod("setVibrationPattern", long[].class);
                method.invoke(mChannel, new Object[] { pattern });
                //mChannel.setVibrationPattern(pattern);
            }

            method = NotificationManager.class.getMethod("createNotificationChannel", clsNotificationChannel);
            method.invoke(mNotificationManager, new Object[] { mChannel });
            //mNotificationManager.createNotificationChannel(mChannel);
            try {
                // For some reason I can't find the app-support-v4.jar for
                // API 26 that includes this method so that I can compile in netbeans.
                // So we use reflection...  If someone coming after can find a newer version
                // that has setChannelId(), please rip out this ugly reflection hack and
                // replace it with a proper call to mNotifyBuilder.setChannelId(id)
                mNotifyBuilder.getClass().getMethod("setChannelId", new Class[] { String.class })
                        .invoke(mNotifyBuilder, new Object[] { id });
            } catch (Exception ex) {
                Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
            }
            //mNotifyBuilder.setChannelId(id);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchMethodException ex) {
            Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(AndroidImplementation.class.getName()).log(Level.SEVERE, null, ex);
        }
        //mNotifyBuilder.setChannelId(id);
    }

}