Example usage for android.content Context NOTIFICATION_SERVICE

List of usage examples for android.content Context NOTIFICATION_SERVICE

Introduction

In this page you can find the example usage for android.content Context NOTIFICATION_SERVICE.

Prototype

String NOTIFICATION_SERVICE

To view the source code for android.content Context NOTIFICATION_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.app.NotificationManager for informing the user of background events.

Usage

From source file:andre.com.datapushandroid.services.FCMService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *//*from  w ww. j  a va  2s .  com*/
private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher).setContentTitle("FCM Message").setContentText(messageBody)
            .setAutoCancel(true).setSound(defaultSoundUri).setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

From source file:ca.rmen.android.poetassistant.PoemAudioExport.java

private void cancelNotifications() {
    Log.v(TAG, "cancelNotifications");
    NotificationManager notificationManager = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(EXPORT_PROGRESS_NOTIFICATION_ID);
    notificationManager.cancel(EXPORT_FINISH_NOTIFICATION_ID);
}

From source file:net.frygo.findmybuddy.GCMIntentService.java

private static void generateAcceptfriendNotification(Context context, String message, String status) {

    Random rand = new Random();
    int x = rand.nextInt();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, customlistview.class);
    if (status.equalsIgnoreCase("accept"))
        message = message + " added you as buddy";
    else//from  w  w  w.j a  v a2s. c om
        message = message + " rejected you as buddy";
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification(R.drawable.logo, message, System.currentTimeMillis());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(x, notification);

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock mWakelock = pm
            .newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, title);
    mWakelock.acquire();

    // Timer before putting Android Device to sleep mode.
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            mWakelock.release();
        }
    };
    timer.schedule(task, 5000);

}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static void hideNotification(Context context, int id) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(id);/*from w w w . java2s.c o  m*/
}

From source file:com.google.android.gcm.demo.app.GcmBroadcastReceiver.java

private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, new Intent(ctx, LLActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx)
            .setSmallIcon(R.drawable.ic_stat_gcm).setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:com.freshplanet.nativeExtensions.C2DMBroadcastReceiver.java

/**
 * Get the parameters from the message and create a notification from it.
 * @param context/*  w w  w.  ja  va2  s. c o  m*/
 * @param intent
 */
public void handleMessage(Context context, Intent intent) {
    try {
        registerResources(context);
        extractColors(context);

        FREContext ctxt = C2DMExtension.context;

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        // icon is required for notification.
        // @see http://developer.android.com/guide/practices/ui_guidelines/icon_design_status_bar.html

        int icon = notificationIcon;
        long when = System.currentTimeMillis();

        // json string

        String parameters = intent.getStringExtra("parameters");
        String facebookId = null;
        JSONObject object = null;
        if (parameters != null) {
            try {
                object = (JSONObject) new JSONTokener(parameters).nextValue();
            } catch (Exception e) {
                Log.d(TAG, "cannot parse the object");
            }
        }
        if (object != null && object.has("facebookId")) {
            facebookId = object.getString("facebookId");
        }

        CharSequence tickerText = intent.getStringExtra("tickerText");
        CharSequence contentTitle = intent.getStringExtra("contentTitle");
        CharSequence contentText = intent.getStringExtra("contentText");

        Intent notificationIntent = new Intent(context, Class.forName(context.getPackageName() + ".AppEntry"));

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

        Notification notification = new Notification(icon, tickerText, when);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

        RemoteViews contentView = new RemoteViews(context.getPackageName(), customLayout);

        contentView.setTextViewText(customLayoutTitle, contentTitle);
        contentView.setTextViewText(customLayoutDescription, contentText);

        contentView.setTextColor(customLayoutTitle, notification_text_color);
        contentView.setFloat(customLayoutTitle, "setTextSize",
                notification_title_size_factor * notification_text_size);
        contentView.setTextColor(customLayoutDescription, notification_text_color);
        contentView.setFloat(customLayoutDescription, "setTextSize",
                notification_description_size_factor * notification_text_size);

        if (facebookId != null) {
            Log.d(TAG, "bitmap not null");
            CreateNotificationTask cNT = new CreateNotificationTask();
            cNT.setParams(customLayoutImageContainer, NotifId, nm, notification, contentView);
            String src = "http://graph.facebook.com/" + facebookId + "/picture?type=normal";
            URL url = new URL(src);
            cNT.execute(url);
        } else {
            Log.d(TAG, "bitmap null");
            contentView.setImageViewResource(customLayoutImageContainer, customLayoutImage);
            notification.contentView = contentView;
            nm.notify(NotifId, notification);
        }
        NotifId++;

        if (ctxt != null) {
            parameters = parameters == null ? "" : parameters;
            ctxt.dispatchStatusEventAsync("COMING_FROM_NOTIFICATION", parameters);
        }

    } catch (Exception e) {
        Log.e(TAG, "Error activating application:", e);
    }
}

From source file:com.miz.service.TraktTvShowsSyncService.java

private void setupNotification() {
    // Setup up notification
    mBuilder = new NotificationCompat.Builder(getApplicationContext());
    mBuilder.setColor(getResources().getColor(R.color.color_primary));
    mBuilder.setSmallIcon(R.drawable.ic_tv_white_24dp);
    mBuilder.setTicker(getString(R.string.syncTvShows));
    mBuilder.setContentTitle(getString(R.string.syncTvShows));
    mBuilder.setContentText(getString(R.string.updatingShowInfo));
    mBuilder.setOngoing(true);//from  w  ww .  ja  va 2  s .co  m
    mBuilder.setOnlyAlertOnce(true);

    // Build notification
    Notification updateNotification = mBuilder.build();

    // Show the notification
    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(NOTIFICATION_ID, updateNotification);

    // Tell the system that this is an ongoing notification, so it shouldn't be killed
    startForeground(NOTIFICATION_ID, updateNotification);
}

From source file:com.iniciacomunicacion.devicenotification.DeviceNotification.java

/**
 * Adds notification/* w w w.  j  av  a  2 s .c o m*/
 * 
 * @param callbackContext, Callback context of the request from Cordova
 * @param title, The title of notification
 * @param message, The content text of the notification
 * @param Id, The unique ID of the notification
 * @param seconds
 */
public void add(CallbackContext callbackContext, String ticker, String title, String message, int id) {

    Resources res = DeviceNotification.context.getResources();
    int ic_launcher = res.getIdentifier("icon", "drawable", cordova.getActivity().getPackageName());

    /* Version 4.x
    NotificationManager notificationManager = (NotificationManager)DeviceNotification.activity.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification.Builder(DeviceNotification.Context)
    .setTicker(ticker)
    .setContentTitle(title)
    .setContentText(message)
    .setSmallIcon(ic_launcher)
    .setWhen(System.currentTimeMillis())
    .setAutoCancel(true)
    .build();
    notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
    notificationManager.notify(id, notification);*/

    //Version 2.x
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(DeviceNotification.activity)
            .setSmallIcon(ic_launcher).setWhen(System.currentTimeMillis()).setContentTitle(title)
            .setContentText(message).setTicker(ticker);
    Intent resultIntent = new Intent(DeviceNotification.activity, DeviceNotification.activity.getClass());
    PendingIntent resultPendingIntent = PendingIntent.getActivity(DeviceNotification.activity, 0, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotifyMgr = (NotificationManager) DeviceNotification.activity
            .getSystemService(android.content.Context.NOTIFICATION_SERVICE);
    mNotifyMgr.notify(id, mBuilder.build());
}

From source file:com.footprint.cordova.plugin.localnotification.Receiver.java

/**
 * Shows the notification//from   w w w  . j  ava  2 s  .co  m
 */
@SuppressWarnings("deprecation")
private void showNotification(Builder notification) {
    NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    int id = 0;

    try {
        id = Integer.parseInt(options.getId());
    } catch (Exception e) {
    }

    if (Build.VERSION.SDK_INT < 16) {
        // build notification for HoneyComb to ICS
        mgr.notify(id, notification.getNotification());
    } else if (Build.VERSION.SDK_INT > 15) {
        // Notification for Jellybean and above
        mgr.notify(id, notification.build());
    }
}

From source file:at.aec.solutions.checkmkagent.AgentService.java

@Override
public void onCreate() {
    super.onCreate();
    Log.v(TAG, "onCreate");

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    m_wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
    m_wakeLock.acquire();//from w w w .j  a va 2 s . c  om

    //Copy busybox binary to app directory
    if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("bbinstalled",
            false)) {
        PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit()
                .putBoolean("bbinstalled", true).commit();

        // There is also String[] Build.SUPPORTED_ABIS from API 21 on and
        // before String Build.CPU_ABI String Build.CPU_ABI2, maybe i should investigate there
        String arch = System.getProperty("os.arch");
        Log.v(TAG, arch);
        if (arch.equals("armv7l")) {
            copyAsset(getAssets(), "bbb/busybox", getApplicationInfo().dataDir + "/busybox");
        }
        if (arch.equals("i686")) {
            copyAsset(getAssets(), "bbb/busybox-i686", getApplicationInfo().dataDir + "/busybox");
        }

        //         copyAsset(getAssets(), "bbb/busybox-x86_64", getApplicationInfo().dataDir+"/busybox-x86_64");
        changeBusyboxPermission();
    }

    socketServerThread = new Thread(new SocketServerThread());
    socketServerThread.setName("CheckMK-Agent ServerThread");
    socketServerThread.start();

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setContentTitle("CheckMK Agent started.")
            .setContentText("Listening on Port " + SERVERPORT + ". Tap to configure.");

    Intent resultIntent = new Intent(this, ConfigureActivity.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(ConfigureActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    Notification notify = mBuilder.build();
    notify.flags |= Notification.FLAG_NO_CLEAR;

    mNotificationManager.notify(mId, notify);
}