Example usage for android.app Notification FLAG_ONLY_ALERT_ONCE

List of usage examples for android.app Notification FLAG_ONLY_ALERT_ONCE

Introduction

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

Prototype

int FLAG_ONLY_ALERT_ONCE

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

Click Source Link

Document

Bit to be bitwise-ored into the #flags field that should be set if you would only like the sound, vibrate and ticker to be played if the notification was not already showing.

Usage

From source file:com.codemobiles.util.CMNotification.java

public static void notify(final Context ctx, final int iconResID, final String remote_image_url,
        final String title, final String desc, final Class<?> receiver) {
    final Handler handler = new Handler();
    new Thread(new Runnable() {

        @Override/*w ww  . j a v a  2  s .co  m*/
        public void run() {
            try {
                remote_picture = BitmapFactory
                        .decodeStream((InputStream) new URL(remote_image_url).getContent());
                // remote_picture = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_launcher);
            } catch (IOException e) {
                e.printStackTrace();
            }

            handler.post(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    mNotificationManager = (NotificationManager) ctx
                            .getSystemService(Context.NOTIFICATION_SERVICE);

                    Notification noti = setNormalNotification(ctx, iconResID, remote_picture, title, desc,
                            receiver);
                    // Notification noti = setBigTextStyleNotification(ctx);
                    noti.defaults |= Notification.DEFAULT_LIGHTS;
                    noti.defaults |= Notification.DEFAULT_VIBRATE;
                    noti.defaults |= Notification.DEFAULT_SOUND;
                    noti.defaults |= Notification.FLAG_AUTO_CANCEL;
                    noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;

                    final int notificationID = new Random().nextInt();
                    noti.when = System.currentTimeMillis() + 1000 * 60;
                    mNotificationManager.notify(notificationID, noti);
                }
            });

        }
    }).start();

}

From source file:net.bither.util.SystemUtil.java

public static void nmNotifyOfWallet(NotificationManager nm, Context context, int notifyId, Intent intent,
        String title, String contentText, int iconId, int rawId) {
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(iconId);/*from   w ww  .ja  v  a  2s .c  o  m*/
    builder.setContentText(contentText);
    builder.setContentTitle(title);

    builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));

    builder.setWhen(System.currentTimeMillis());
    Notification notification = null;
    if (ServiceUtil.isNoPrompt(System.currentTimeMillis())) {
        notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
        notification.sound = null;
    } else {
        builder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/" + rawId));
        notification = builder.build();
        notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.ledARGB = 0xFF84E4FA;
        notification.ledOnMS = 3000;
        notification.ledOffMS = 2000;
    }

    nm.notify(notifyId, notification);

}

From source file:com.gelakinetic.mtgfam.helpers.DbUpdaterService.java

@Override
public void onCreate() {
    super.onCreate();
    mPrefAdapter = new PreferencesAdapter(this);
    try {/*  w w  w .  j  a  v a2 s.  co  m*/
        mDbHelper = new CardDbAdapter(this);
    } catch (FamiliarDbException e) {
        mDbHelper = null;
        return; // couldnt open the db, might as well return
    }
    mDbHelper.close(); // close the newly opened db so we can transact it later
    mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Intent intent = new Intent(this, MainActivity.class);
    mNotificationIntent = PendingIntent.getActivity(this, 0, intent, 0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getApplicationContext());
    mUpdateNotification = builder.setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.update_notification))
            .setSmallIcon(R.drawable.rt_notification_icon).setContentIntent(mNotificationIntent)
            .setWhen(System.currentTimeMillis()).build();

    mUpdateNotification.flags |= Notification.FLAG_ONGOING_EVENT;
    mUpdateNotification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
}

From source file:net.bither.util.SystemUtil.java

public static void nmNotifyDefault(NotificationManager nm, Context context, int notifyId, Intent intent,
        String title, String contentText, int iconId) {
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(iconId);//  w  w w.  j  a va  2s. c  om
    builder.setContentText(contentText);
    builder.setContentTitle(title);

    builder.setContentIntent(PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));

    builder.setWhen(System.currentTimeMillis());
    Notification notification = null;

    notification = builder.build();
    notification.defaults = Notification.DEFAULT_SOUND;
    notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0xFF84E4FA;
    notification.ledOnMS = 3000;
    notification.ledOffMS = 2000;
    nm.notify(notifyId, notification);

}

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  av  a2  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:net.dian1.player.service.DownloadService.java

private void displayNotifcation(DownloadJob job) {
    String notificationMessage = job.getPlaylistEntry().getMusic().getName() + " - "
            + job.getPlaylistEntry().getAlbum().getArtistName();
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, DownloadActivity.class),
            0);/* w  w  w.  ja  v a  2  s  .  co  m*/
    Resources res = getResources();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentIntent(contentIntent).setSmallIcon(android.R.drawable.stat_sys_download_done)
            .setLargeIcon(BitmapFactory.decodeResource(res, android.R.drawable.stat_sys_download_done))
            .setTicker(notificationMessage).setWhen(System.currentTimeMillis()).setAutoCancel(true)
            .setContentTitle(getString(R.string.downloaded)).setContentText(notificationMessage);
    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;

    mNotificationManager.notify(DOWNLOAD_NOTIFY_ID, notification);
}

From source file:ca.rmen.android.networkmonitor.app.service.NetMonNotification.java

/**
 * Shows a notification with the given ticker text and content text. The icon is a warning icon, and the notification title is the app name. Tapping on the
 * notification opens the given activity.
 *///from w  w w. java2s.c  om
private static void showNotification(Context context, int notificationId, int tickerTextId, int contentTextId,
        Class<?> activityClass) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.drawable.ic_stat_warning);
    builder.setAutoCancel(true);
    builder.setTicker(context.getString(tickerTextId));
    builder.setContentTitle(context.getString(R.string.app_name));
    builder.setContentText(context.getString(contentTextId));
    builder.setAutoCancel(false);
    Uri uri = NetMonPreferences.getInstance(context).getNotificationSoundUri();
    builder.setSound(uri);
    builder.setContentIntent(PendingIntent.getActivity(context, 0, new Intent(context, activityClass),
            PendingIntent.FLAG_UPDATE_CURRENT));
    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_ONLY_ALERT_ONCE;
    notification.ledARGB = 0xFFffff00;
    notification.ledOnMS = 300;
    notification.ledOffMS = 2000;
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(notificationId, notification);
}

From source file:com.hmatalonga.greenhub.util.Notifier.java

public static void newMessageAlert(final Context context) {
    if (sNotificationManager == null) {
        sNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }// w  w w  .jav a 2  s.  c  o m

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_email_white_24dp)
            .setContentTitle(context.getString(R.string.notif_new_message))
            .setContentText(context.getString(R.string.notif_open_inbox)).setAutoCancel(true).setOngoing(false)
            .setLights(Color.GREEN, 500, 2000).setVibrate(new long[] { 0, 800, 1500 })
            .setPriority(SettingsUtils.fetchNotificationsPriority(context));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, InboxActivity.class);

    // The stack builder object will contain an artificial back stack for the
    // started Activity.
    // This ensures that navigating backward from the Activity leads out of
    // your application to the Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Adds the back stack for the Intent (but not the Intent itself)
    stackBuilder.addParentStack(InboxActivity.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);

    // Because the ID remains unchanged, the existing notification is updated.
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    sNotificationManager.notify(Config.NOTIFICATION_MESSAGE_NEW, notification);
}

From source file:hmatalonga.greenhub.util.Notifier.java

public static void batteryWarningTemperature(final Context context) {
    if (sNotificationManager == null) {
        sNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    }/*from w  ww.  j av  a 2s . c o m*/

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_alert_circle_white_24dp).setContentTitle("Battery warning")
            .setContentText("Temperature is getting warm").setAutoCancel(true).setOngoing(false)
            .setLights(Color.YELLOW, 500, 2000).setVibrate(new long[] { 0, 400, 1000 })
            .setPriority(SettingsUtils.fetchNotificationsPriority(context));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
    }

    // Because the ID remains unchanged, the existing notification is updated.
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    sNotificationManager.notify(Config.NOTIFICATION_TEMPERATURE_WARNING, notification);
}

From source file:com.futureplatforms.kirin.extensions.localnotifications.LocalNotificationsBackend.java

@SuppressWarnings("deprecation")
public Notification createNotification(Bundle settings, JSONObject obj) {

    // notifications[i] = api.normalizeAPI({
    // 'string': {
    // mandatory: ['title', 'body'],
    // defaults: {'icon':'icon'}
    // },//from w  w w.  j a  v  a 2s  . com
    //
    // 'number': {
    // mandatory: ['id', 'timeMillisSince1970'],
    // // the number of ms after which we start prioritising more recent
    // things above you.
    // defaults: {'epsilon': 1000 * 60 * 24 * 365}
    // },
    //
    // 'boolean': {
    // defaults: {
    // 'vibrate': false,
    // 'sound': false
    // }
    // }

    int icon = settings.getInt("notification_icon", -1);
    if (icon == -1) {
        Log.e(C.TAG, "Need a notification_icon resource in the meta-data of LocalNotificationsAlarmReceiver");
        return null;
    }

    Notification n = new Notification();
    n.icon = icon;
    n.flags = Notification.FLAG_ONLY_ALERT_ONCE | Notification.FLAG_AUTO_CANCEL;
    long alarmTime = obj.optLong("timeMillisSince1970");
    long displayTime = obj.optLong("displayTimestamp", alarmTime);
    n.when = displayTime;
    n.tickerText = obj.optString("body");
    n.setLatestEventInfo(mContext, obj.optString("title"), obj.optString("body"), null);

    if (obj.optBoolean("vibrate")) {
        n.defaults |= Notification.DEFAULT_VIBRATE;
    }
    if (obj.optBoolean("sound")) {
        n.defaults |= Notification.DEFAULT_SOUND;
    }

    String uriString = settings.getString("content_uri_prefix");
    if (uriString == null) {
        Log.e(C.TAG, "Need a content_uri_prefix in the meta-data of LocalNotificationsAlarmReceiver");
        return null;
    }

    if (uriString.contains("%d")) {
        uriString = String.format(uriString, obj.optInt("id"));
    }
    Uri uri = Uri.parse(uriString);

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setData(uri);
    n.contentIntent = PendingIntent.getActivity(mContext, 23, intent, PendingIntent.FLAG_ONE_SHOT);
    return n;
}