ti.notifications.TiNotificationsModule.java Source code

Java tutorial

Introduction

Here is the source code for ti.notifications.TiNotificationsModule.java

Source

/**
 * This file was auto-generated by the Titanium Module SDK helper for Android
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, 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.notifications;

import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.kroll.common.TiConfig;
import org.appcelerator.titanium.proxy.IntentProxy;

import sun.rmi.runtime.NewThreadAction;
import android.app.Notification;
import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;

@Kroll.module(name = "TiNotifications", id = "ti.notifications")
public class TiNotificationsModule extends KrollModule {

    // Standard Debugging variables
    private static final String LCAT = "TiNotificationsModule";
    private static final boolean DBG = TiConfig.LOGD;

    // You can define constants with @Kroll.constant, for example:
    // @Kroll.constant public static final String EXTERNAL_NAME = value;

    public TiNotificationsModule() {
        super();
    }

    @Kroll.onAppCreate
    public static void onAppCreate(TiApplication app) {
        Log.d(LCAT, "inside onAppCreate");
        // put module init code that needs to run when the application is created
    }

    // Methods
    @Kroll.method
    public String example() {
        Log.d(LCAT, "example called");
        return "hello world";
    }

    public PendingIntent pendingIntentLauncherApp(String className, int notificationId, String packageName,
            String source) {
        Context context = TiApplication.getInstance().getApplicationContext();
        Intent notificationIntent = new Intent("action" + notificationId);

        notificationIntent.setClassName(context, className);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        notificationIntent.setPackage(packageName);

        notificationIntent.putExtra("ntfId", notificationId);
        notificationIntent.putExtra("notifications", notificationId);
        notificationIntent.putExtra("source", source);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        return pendingIntent;
    }

    public PendingIntent pendingIntentOpenUrl(String url) {
        Context context = TiApplication.getInstance().getApplicationContext();
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        return pendingIntent;
    }

    @Kroll.method
    public void addActionUrl(int notificationId, int smallIcon, String title, String message, String summaryText,
            int iconAction, String actionMessage) {

        Context context = TiApplication.getInstance().getApplicationContext();
        NotificationCompat.Builder nc = new NotificationCompat.Builder(context);
        NotificationCompat.BigTextStyle bigTextNotification = new NotificationCompat.BigTextStyle();
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        nc.setSmallIcon(smallIcon);
        nc.setAutoCancel(true);
        nc.setContentTitle(title);
        nc.setContentText(message);

        nc.setDefaults(Notification.DEFAULT_ALL);

        nc.addAction(iconAction, actionMessage, pendingIntentOpenUrl(summaryText));

        bigTextNotification.setBigContentTitle(title);
        bigTextNotification.bigText(message);
        bigTextNotification.setSummaryText(summaryText);

        nc.setStyle(bigTextNotification);

        nm.notify(notificationId, nc.build());
    }

    @Kroll.method
    public void addActionReport(int notificationId, int smallIcon, String title, String message, String summaryText,
            int iconAction, String actionMessage, String className, String packageName) {

        Context context = TiApplication.getInstance().getApplicationContext();
        NotificationCompat.Builder nc = new NotificationCompat.Builder(context);
        NotificationCompat.BigTextStyle bigTextNotification = new NotificationCompat.BigTextStyle();
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent pendingIntent = pendingIntentLauncherApp(className, notificationId, packageName, summaryText);

        nc.setSmallIcon(smallIcon);
        nc.setAutoCancel(true);
        nc.setContentTitle(title);
        nc.setContentText(message);

        nc.setDefaults(Notification.DEFAULT_ALL);

        nc.addAction(iconAction, actionMessage, pendingIntent);

        bigTextNotification.setBigContentTitle(title);
        bigTextNotification.bigText(message);
        bigTextNotification.setSummaryText(summaryText);

        nc.setStyle(bigTextNotification);

        nm.notify(notificationId, nc.build());
    }

    // Properties
    @Kroll.getProperty
    public String getExampleProp() {
        Log.d(LCAT, "get example property");
        return "hello world";
    }

    @Kroll.setProperty
    public void setExampleProp(String value) {
        Log.d(LCAT, "set example property: " + value);
    }

}