Example usage for android.app Notification FLAG_FOREGROUND_SERVICE

List of usage examples for android.app Notification FLAG_FOREGROUND_SERVICE

Introduction

In this page you can find the example usage for android.app Notification FLAG_FOREGROUND_SERVICE.

Prototype

int FLAG_FOREGROUND_SERVICE

To view the source code for android.app Notification FLAG_FOREGROUND_SERVICE.

Click Source Link

Document

Bit to be bitwise-ored into the #flags field that should be set if this notification represents a currently running service.

Usage

From source file:com.money.manager.ex.sync.SyncNotificationFactory.java

/**
 * Get the builder of a notification for download
 * @return notification/*w w  w  .j a  va  2s .  c o m*/
 */
public Notification getNotificationForDownload() {
    NotificationCompat.Builder notification = new NotificationCompat.Builder(getContext())
            .setContentTitle(getContext().getString(R.string.sync_notification_title)).setAutoCancel(false)
            .setDefaults(Notification.FLAG_FOREGROUND_SERVICE)
            .setContentText(getContext().getString(R.string.sync_downloading))
            //.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_action_dropbox_dark))
            .setSmallIcon(R.drawable.ic_stat_notification)
            .setColor(getContext().getResources().getColor(R.color.md_primary));

    // only for previous version!
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        Intent intent = new Intent(getContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(getContext(), 0, intent, 0);
        notification.setContentIntent(pendingIntent);
    }

    return notification.build();
}

From source file:com.aniruddhc.acemusic.player.Services.AutoFetchAlbumArtService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //Launch a notification to set the service as a foreground service.
    builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setContentTitle(getResources().getString(R.string.downloading_missing_cover_art));
    builder.setTicker(getResources().getString(R.string.downloading_missing_cover_art));
    builder.setContentText(null);// w  ww.j av  a 2s . c  o m

    notification = builder.build();
    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
    notification.flags |= Notification.FLAG_NO_CLEAR;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;

    startForeground(NOTIFICATION_ID, notification);

    //Call the AsyncTask that checks for missing art and downloads them.
    AsyncAutoGetAlbumArtTask task = new AsyncAutoGetAlbumArtTask(mContext,
            SettingsActivity____.mSettingsActivity);
    task.execute();

    return START_STICKY;
}

From source file:com.ademsha.appnotifico.NotificationHelper.java

public static void notifyForForegroundService(final Service service,
        final PreparedNotification preparedNotification) {

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(preparedNotification.getContext())
            .setPriority(NotificationCompat.PRIORITY_MIN).setOngoing(true).setAutoCancel(false)
            .setOnlyAlertOnce(true);// ww w .j  a v  a2  s  .c o  m

    if (preparedNotification.getPendingIntentForActivity() != null) {
        builder.setContentIntent(preparedNotification.getPendingIntentForService());
    }

    builder.setContentTitle(preparedNotification.getTitle());
    builder.setContentText(preparedNotification.getText());

    if (preparedNotification.getLargeIcon() != null) {
        builder.setLargeIcon(preparedNotification.getLargeIcon());
    }

    if (preparedNotification.getSmallIcon() > 0) {
        builder.setSmallIcon(preparedNotification.getSmallIcon());
    }

    builder.setTicker(preparedNotification.getTicker());

    if (!preparedNotification.getExpanded().equals("")
            && !preparedNotification.getExpandedSummary().equals("")) {
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(preparedNotification.getExpanded())
                .setBigContentTitle(preparedNotification.getTitle())
                .setSummaryText(preparedNotification.getExpandedSummary()));
    }

    if (preparedNotification.getPendingIntentForActivity() != null) {
        builder.setContentIntent(preparedNotification.getPendingIntentForActivity());
    }

    Notification notification;
    notification = builder.build();
    notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE
            | Notification.FLAG_NO_CLEAR;
    service.startForeground(10000, notification);
}

From source file:com.tenforwardconsulting.cordova.bgloc.AbstractLocationService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Received start id " + startId + ": " + intent);
    if (intent != null) {
        config = (Config) intent.getParcelableExtra("config");
        activity = intent.getStringExtra("activity");
        Log.d(TAG, "Got activity" + activity);

        // Build a Notification required for running service in foreground.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(config.getNotificationTitle());
        builder.setContentText(config.getNotificationText());
        builder.setSmallIcon(android.R.drawable.ic_menu_mylocation);
        if (config.getNotificationIcon() != null) {
            builder.setSmallIcon(getPluginResource(config.getSmallNotificationIcon()));
            builder.setLargeIcon(BitmapFactory.decodeResource(getApplication().getResources(),
                    getPluginResource(config.getLargeNotificationIcon())));
        }//w  w w  . j a v  a  2  s.  c o  m
        if (config.getNotificationIconColor() != null) {
            builder.setColor(this.parseNotificationIconColor(config.getNotificationIconColor()));
        }

        setClickEvent(builder);

        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE
                | Notification.FLAG_NO_CLEAR;
        startForeground(startId, notification);
    }
    Log.i(TAG, config.toString());
    Log.i(TAG, "- activity: " + activity);

    //We want this service to continue running until it is explicitly stopped
    return START_STICKY;
}

From source file:com.tenforwardconsulting.cordova.bgloc.LocationService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Received start id " + startId + ": " + intent);

    // config = Config.fromByteArray(intent.getByteArrayExtra("config"));
    if (intent.hasExtra("config")) {
        config = (Config) intent.getParcelableExtra("config");
    } else {//from  w ww .jav  a2s  .  c  o  m
        config = new Config();
    }

    ServiceProviderFactory spf = new ServiceProviderFactory(this, config);
    provider = spf.getInstance(config.getServiceProvider());
    provider.onCreate();

    if (config.getStartForeground()) {
        // Build a Notification required for running service in foreground.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(config.getNotificationTitle());
        builder.setContentText(config.getNotificationText());
        if (config.getSmallNotificationIcon() != null) {
            builder.setSmallIcon(getPluginResource(config.getSmallNotificationIcon()));
        } else {
            builder.setSmallIcon(android.R.drawable.ic_menu_mylocation);
        }
        if (config.getLargeNotificationIcon() != null) {
            builder.setLargeIcon(BitmapFactory.decodeResource(getApplication().getResources(),
                    getPluginResource(config.getLargeNotificationIcon())));
        }
        if (config.getNotificationIconColor() != null) {
            builder.setColor(this.parseNotificationIconColor(config.getNotificationIconColor()));
        }

        setClickEvent(builder);

        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE
                | Notification.FLAG_NO_CLEAR;
        startForeground(startId, notification);
    }

    provider.startRecording();

    //We want this service to continue running until it is explicitly stopped
    return START_REDELIVER_INTENT;
}

From source file:org.jitsi.android.gui.util.AndroidUtils.java

/**
 * Shows an alert dialog for the given context and a title given by
 * <tt>titleId</tt> and message given by <tt>messageId</tt>.
 *
 * @param context the android <tt>Context</tt>
 * @param notificationID the identifier of the notification to update
 * @param title the title of the message
 * @param message the message/*from   www. j a va2 s .  c  om*/
 * @param date the date on which the event corresponding to the notification
 * happened
 */
public static void updateGeneralNotification(Context context, int notificationID, String title, String message,
        long date) {
    // Filter out the same subsequent notifications
    if (lastNotificationText != null && lastNotificationText.equals(message)) {
        return;
    }

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context).setContentTitle(title)
            .setContentText(message).setWhen(date).setSmallIcon(R.drawable.notificationicon);

    nBuilder.setContentIntent(JitsiApplication.getJitsiIconIntent());
    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = nBuilder.build();

    notification.flags = Notification.FLAG_ONLY_ALERT_ONCE & Notification.FLAG_FOREGROUND_SERVICE
            & Notification.FLAG_NO_CLEAR;

    // mId allows you to update the notification later on.
    mNotificationManager.notify(notificationID, notification);

    lastNotificationText = message;
}

From source file:org.kde.kdeconnect.Plugins.NotificationsPlugin.NotificationsPlugin.java

public void sendNotification(StatusBarNotification statusBarNotification, boolean requestAnswer) {

    Notification notification = statusBarNotification.getNotification();
    AppDatabase appDatabase = new AppDatabase(context);

    if ((notification.flags & Notification.FLAG_FOREGROUND_SERVICE) != 0
            || (notification.flags & Notification.FLAG_ONGOING_EVENT) != 0
            || (notification.flags & Notification.FLAG_LOCAL_ONLY) != 0) {
        //This is not a notification we want!
        return;/*from ww  w  .jav a 2  s  .c  o m*/
    }

    appDatabase.open();
    if (!appDatabase.isEnabled(statusBarNotification.getPackageName())) {
        return;
        // we dont want notification from this app
    }
    appDatabase.close();

    String key = getNotificationKeyCompat(statusBarNotification);
    String packageName = statusBarNotification.getPackageName();
    String appName = AppsHelper.appNameLookup(context, packageName);

    if ("com.facebook.orca".equals(packageName) && (statusBarNotification.getId() == 10012)
            && "Messenger".equals(appName) && notification.tickerText == null) {
        //HACK: Hide weird Facebook empty "Messenger" notification that is actually not shown in the phone
        return;
    }

    if (packageName.equals("com.google.android.googlequicksearchbox")) {
        //HACK: Hide Google Now notifications that keep constantly popping up (and without text because we don't know how to read them properly)
        return;
    }

    NetworkPackage np = new NetworkPackage(NetworkPackage.PACKAGE_TYPE_NOTIFICATION);

    if (packageName.equals("org.kde.kdeconnect_tp")) {
        //Make our own notifications silent :)
        np.set("silent", true);
        np.set("requestAnswer", true); //For compatibility with old desktop versions of KDE Connect that don't support "silent"
    }
    /*
            if (sendIcons) {
    try {
        Drawable drawableAppIcon = AppsHelper.appIconLookup(context, packageName);
        Bitmap appIcon = ImagesHelper.drawableToBitmap(drawableAppIcon);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        if (appIcon.getWidth() > 128) {
            appIcon = Bitmap.createScaledBitmap(appIcon, 96, 96, true);
        }
        appIcon.compress(Bitmap.CompressFormat.PNG, 90, outStream);
        byte[] bitmapData = outStream.toByteArray();
        np.setPayload(bitmapData);
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("NotificationsPlugin", "Error retrieving icon");
    }
            }
    */
    np.set("id", key);
    np.set("appName", appName == null ? packageName : appName);
    np.set("isClearable", statusBarNotification.isClearable());
    np.set("ticker", getTickerText(notification));
    np.set("time", Long.toString(statusBarNotification.getPostTime()));
    if (requestAnswer) {
        np.set("requestAnswer", true);
        np.set("silent", true);
    }

    device.sendPackage(np);
}

From source file:com.gsma.rcs.notif.RcsServiceNotifManager.java

private void addImsConnectionNotification(boolean connected, String label) {
    String title = this.getString(R.string.notification_title_rcs_service);
    Notification notif = buildImsConnectionNotification(null, title, label, connected);
    notif.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIF_ID, notif);
}

From source file:arun.com.chromer.webheads.WebHeadService.java

@NonNull
@Override/* w ww . ja  v  a 2  s. com*/
public Notification getNotification() {
    if (Utils.ANDROID_OREO) {
        final NotificationChannel channel = new NotificationChannel(WebHeadService.class.getName(),
                getString(R.string.web_heads_service), NotificationManager.IMPORTANCE_MIN);
        channel.setDescription(getString(R.string.app_detection_notification_channel_description));
        final NotificationManager notificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.createNotificationChannel(channel);
        }
    }
    final PendingIntent contentIntent = PendingIntent.getBroadcast(this, 0,
            new Intent(ACTION_STOP_WEBHEAD_SERVICE), FLAG_UPDATE_CURRENT);
    final PendingIntent contextActivity = PendingIntent.getBroadcast(this, 0,
            new Intent(ACTION_OPEN_CONTEXT_ACTIVITY), FLAG_UPDATE_CURRENT);
    final PendingIntent newTab = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_OPEN_NEW_TAB),
            FLAG_UPDATE_CURRENT);
    Notification notification = new NotificationCompat.Builder(this, WebHeadService.class.getName())
            .setSmallIcon(R.drawable.ic_chromer_notification).setPriority(PRIORITY_MIN)
            .setContentText(getString(R.string.tap_close_all))
            .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
            .addAction(R.drawable.ic_add, getText(R.string.open_new_tab), newTab)
            .addAction(R.drawable.ic_list, getText(R.string.manage), contextActivity)
            .setContentTitle(getString(R.string.web_heads_service)).setContentIntent(contentIntent)
            .setAutoCancel(false).setLocalOnly(true).build();
    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
    return notification;
}

From source file:com.gsma.rcs.ri.RcsServiceNotifManager.java

private void addImsConnectionNotification(boolean connected, String label) {
    Intent intent = new Intent(ACTION_VIEW_SETTINGS);
    PendingIntent contentIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
    String title = this.getString(R.string.notification_title_rcs_service);
    Notification notif = buildImsConnectionNotification(contentIntent, title, label, connected);
    notif.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_FOREGROUND_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIF_ID, notif);
}