Example usage for android.app PendingIntent FLAG_ONE_SHOT

List of usage examples for android.app PendingIntent FLAG_ONE_SHOT

Introduction

In this page you can find the example usage for android.app PendingIntent FLAG_ONE_SHOT.

Prototype

int FLAG_ONE_SHOT

To view the source code for android.app PendingIntent FLAG_ONE_SHOT.

Click Source Link

Document

Flag indicating that this PendingIntent can be used only once.

Usage

From source file:com.vedant.hereami.chatfolder.MyFirebaseMessagingService.java

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, 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("Firebase Push Notification")
            .setContentText(messageBody).setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(0, notificationBuilder.build());
}

From source file:be.ppareit.swiftp.gui.FsNotification.java

private void setupNotification(Context context) {
    Cat.d("Setting up the notification");
    // Get NotificationManager reference
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager nm = (NotificationManager) context.getSystemService(ns);

    // get ip address
    InetAddress address = FsService.getLocalInetAddress();
    if (address == null) {
        Cat.w("Unable to retrieve the local ip address");
        return;//from   w w  w  . jav  a2s  .c  o m
    }
    String iptext = "ftp://" + address.getHostAddress() + ":" + FsSettings.getPortNumber() + "/";

    // Instantiate a Notification
    int icon = R.mipmap.notification;
    CharSequence tickerText = String.format(context.getString(R.string.notification_server_starting), iptext);
    long when = System.currentTimeMillis();

    // Define Notification's message and Intent
    CharSequence contentTitle = context.getString(R.string.notification_title);
    CharSequence contentText = String.format(context.getString(R.string.notification_text), iptext);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    int stopIcon = android.R.drawable.ic_menu_close_clear_cancel;
    CharSequence stopText = context.getString(R.string.notification_stop_text);
    Intent stopIntent = new Intent(FsService.ACTION_STOP_FTPSERVER);
    PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 0, stopIntent,
            PendingIntent.FLAG_ONE_SHOT);

    int preferenceIcon = android.R.drawable.ic_menu_preferences;
    CharSequence preferenceText = context.getString(R.string.notif_settings_text);
    Intent preferenceIntent = new Intent(context, MainActivity.class);
    preferenceIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent preferencePendingIntent = PendingIntent.getActivity(context, 0, preferenceIntent, 0);

    Notification notification = new NotificationCompat.Builder(context).setContentTitle(contentTitle)
            .setContentText(contentText).setContentIntent(contentIntent).setSmallIcon(icon)
            .setTicker(tickerText).setWhen(when).setOngoing(true)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setCategory(NotificationCompat.CATEGORY_SERVICE).setPriority(NotificationCompat.PRIORITY_MAX)
            .addAction(stopIcon, stopText, stopPendingIntent)
            .addAction(preferenceIcon, preferenceText, preferencePendingIntent).setShowWhen(false).build();

    // Pass Notification to NotificationManager
    nm.notify(NOTIFICATION_ID, notification);

    Cat.d("Notification setup done");
}

From source file:com.aluvi.android.services.push.AluviPushNotificationListenerService.java

private void sendNotification(String message) {
    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(getString(R.string.app_name))
            .setContentText(message).setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}

From source file:com.scsy150.chat.activity.BaseActivity.java

/**
 * ???????????// w  w w . java 2  s.c om
 * ????
 * @param message
 */
protected void notifyNewMessage(EMMessage message) {
    //????(app??demo??)
    //?setShowNotificationInbackgroup:false(false???sdk??)
    if (!EasyUtils.isAppRunningForeground(this)) {
        return;
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(getApplicationInfo().icon).setWhen(System.currentTimeMillis()).setAutoCancel(true);

    String ticker = CommonUtils.getMessageDigest(message, this);
    String st = getResources().getString(R.string.app_name);
    if (message.getType() == Type.TXT)
        ticker = ticker.replaceAll("\\[.{2,3}\\]", st);
    //????
    mBuilder.setTicker(message.getFrom() + ": " + ticker);

    //pendingintent?2.3bug
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, notifiId, intent,
            PendingIntent.FLAG_ONE_SHOT);
    mBuilder.setContentIntent(pendingIntent);

    Notification notification = mBuilder.build();
    notificationManager.notify(notifiId, notification);
    notificationManager.cancel(notifiId);
}

From source file:com.feytuo.chat.activity.BaseActivity.java

/**
 * ???????????/*from  w w w  .j av a  2  s  .c o m*/
 * ????
 * @param message
 */
public void notifyNewMessage(EMMessage message) {
    //????(app??demo??)
    //?setShowNotificationInbackgroup:false(false???sdk??)
    if (!EasyUtils.isAppRunningForeground(this)) {
        return;
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(getApplicationInfo().icon).setWhen(System.currentTimeMillis()).setAutoCancel(true);

    String ticker = CommonUtils.getMessageDigest(message, this);
    String st = getResources().getString(R.string.expression);
    if (message.getType() == Type.TXT)
        ticker = ticker.replaceAll("\\[.{2,3}\\]", st);
    //????
    mBuilder.setTicker(message.getFrom() + ": " + ticker);

    //pendingintent?2.3bug
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, notifiId, intent,
            PendingIntent.FLAG_ONE_SHOT);
    mBuilder.setContentIntent(pendingIntent);

    Notification notification = mBuilder.build();
    notificationManager.notify(notifiId, notification);
    notificationManager.cancel(notifiId);
}

From source file:com.cardillsports.vithushan.cardillsportsandroid.service.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *//*from  w w w . j  av a  2 s.  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.drawable.cslogo).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:com.belatrix.events.utils.fcm.EventsFirebaseMessagingService.java

private void sendNotification(String messageTitle, String messageBody) {
    Intent intent = MainActivity.makeIntent(this);
    intent.putExtra(MainActivity.PARAM_FROM_NOTIFICATION, true);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle(messageTitle);
    bigTextStyle.bigText(messageBody);/*from  ww  w .j  av a2s .c om*/

    Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_launcher);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.bx_connect_white).setLargeIcon(icon).setStyle(bigTextStyle)
            .setContentText(messageBody).setContentTitle(messageTitle).setAutoCancel(true)
            .setContentIntent(pendingIntent).setSound(alarmSound).setLights(0xFF8F0300, 1000, 200)
            .setPriority(Notification.PRIORITY_MAX);

    //for vibration
    Vibrator v = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
    v.vibrate(1000);

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

    long time = new Date().getTime();
    String tmpStr = String.valueOf(time);
    String last4Str = tmpStr.substring(tmpStr.length() - 5);
    int notificationId = Integer.valueOf(last4Str);

    notificationManager.notify(notificationId, notificationBuilder.build());
}

From source file:com.shivaraj.friendz.shivaraj.CloudMessaging.MyFirebaseMessagingService.java

/**
 * Create and show a simple notification containing the received FCM message.
 *
 * @param messageBody FCM message body received.
 *///from www  .ja va2  s . co  m
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("pokemon").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:com.example.stacky.ScheduledMaintenanceService.java

protected void displayNotificationOne() {

    System.out.println("timing is: " + (System.currentTimeMillis() - lastTime) / 1000 + " Sec..");
    lastTime = System.currentTimeMillis();
    // Invoking the default notification service
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

    mBuilder.setContentTitle("New Message with Alarm Manager");
    mBuilder.setContentText("ScheduledMaintenanceService run!!!");
    mBuilder.setTicker("New Message Received!");
    mBuilder.setSmallIcon(R.drawable.ic_launcher);

    // Increase notification number every time a new notification arrives 
    mBuilder.setNumber(++numMessagesOne);

    // Creates an explicit intent for an Activity in your app 
    Intent resultIntent = new Intent(this, NotificationOne.class);
    resultIntent.putExtra("notificationId", notificationIdOne);

    //This ensures that navigating backward from the Activity leads out of the app to Home page
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // Adds the back stack for the Intent
    stackBuilder.addParentStack(NotificationOne.class);

    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT //can only be used once
    );//  w  w  w .  j av  a 2  s .co  m
    // start the activity when the user clicks the notification text
    mBuilder.setContentIntent(resultPendingIntent);

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

    // pass the Notification object to the system 
    myNotificationManager.notify(notificationIdOne, mBuilder.build());
}

From source file:com.bayapps.android.robophish.gcm.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 *///w  w w .  j  a v  a 2s .  c o m
private void showNotification(String title, String subtitle, String mediaId) {
    Intent intent = new Intent(this, MusicPlayerActivity.class);
    intent.putExtra(MusicPlayerActivity.EXTRA_START_FULLSCREEN, false);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    //TEST
    intent.setAction(MediaStore.INTENT_ACTION_MEDIA_SEARCH);
    intent.putExtra("title", title);
    intent.putExtra("subtitle", subtitle);
    intent.putExtra("showid", "__TRACKS_BY_SHOW__/" + mediaId);

    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.drawable.ic_allmusic_black_24dp).setContentTitle("There's a new Phish show!")
            .setContentText(title + ", " + subtitle).setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(Integer.parseInt(mediaId), notificationBuilder.build());
}