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:gcm.play.android.samples.com.gcm.MyGcmListenerService.java

private void sendNotification(String message) {
    try {/*from w  ww .j a  v  a2  s . com*/
        defaultSoundUri = Uri.parse(readFile("ringtone.txt"));
    } catch (Exception exp) {
        defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }

    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);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_ic_notification).setContentTitle("Notifier")
            .setContentText(message).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.cliqz.browser.gcm.MessageListenerService.java

/**
 * Create and show a simple notification containing the received GCM message. Does nothing if
 * the notifications are disabled in the preferences.
 *
 * @param title GCM message received./*from  w  w w .  j av  a 2s .co  m*/
 * @param url   url
 */
private void sendNewsNotification(int newType, String title, String url, String country) {
    if (!preferenceManager.getNewsNotificationEnabled() || country == null
            || !country.equals(preferenceManager.getCountryChoice().countryCode)) {
        return;
    }

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(url));
    intent.putExtra(Constants.NOTIFICATION_CLICKED, true);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Intent deleteIntent = new Intent(this, NotificationDismissedReceiver.class);
    PendingIntent deletePendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0,
            deleteIntent, 0);

    final Uri uri = Uri.parse(url);
    final String host = uri.getHost();
    final String domain = UrlUtils.getTopDomain(url);
    final Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    final NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
    style.bigText(title);
    final NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification_news).setContentTitle(domain)
            .setCategory(NotificationCompat.CATEGORY_RECOMMENDATION).setContentText(title).setAutoCancel(true)
            .setSound(defaultSoundUri).setContentIntent(pendingIntent).setDeleteIntent(deletePendingIntent)
            .setStyle(style);

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

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

From source file:com.winsuk.pebbletype.WatchCommunication.java

public void receiveData(final Context context, final int transactionId, final PebbleDictionary data) {
    PebbleKit.sendAckToPebble(context, transactionId);

    if (data.contains(KEY_REQUEST_THREAD_LIST)) {
        sendThreadList(context);//from w w  w . j av a 2 s .  c  o m
    }

    if (data.contains(KEY_SEND_MESSAGE)) {
        String str = data.getString(KEY_SEND_MESSAGE);
        boolean demoMode = str.startsWith(";");

        String address = str.substring(0, str.indexOf(";"));
        String message = str.substring(str.indexOf(";") + 1, str.length());

        if (!demoMode) {
            SmsManager sms = SmsManager.getDefault();

            // Tried to use getService here which makes more sense, but that didn't work
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(ACTION_SMS_REPLY),
                    PendingIntent.FLAG_ONE_SHOT);

            sms.sendTextMessage(address, null, message, pi, null);
        }
    }
}

From source file:com.survey.android.c2dm.GcmNotificationReceiver.java

@SuppressWarnings("deprecation")
private void sendNotification(Intent intent) {

    final String testResult = intent.getStringExtra("message");
    final String surveyId = intent.getStringExtra("survey_id");

    try {//w  w  w . j  a v  a 2  s.c o  m
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(ns);

        String mDrawableName = "c2dm_icon";
        int icon = this.getResources().getIdentifier(mDrawableName, "drawable", Prefs.PACKAGE);
        CharSequence tickerText = "Survey notification";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);

        CharSequence contentTitle = "Survey notification";
        int NOTIFICATION_ID = 1;
        Intent notificationIntent = new Intent(this, com.survey.android.view.themed.NotificationActivity.class);
        notificationIntent.putExtra("notification_id", NOTIFICATION_ID);
        notificationIntent.putExtra("survey_id", surveyId);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_ONE_SHOT + PendingIntent.FLAG_UPDATE_CURRENT);

        notification.setLatestEventInfo(this, contentTitle, testResult, contentIntent);

        mNotificationManager.notify(NOTIFICATION_ID, notification);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:kr.ds.say.MyFirebaseMessagingService.java

private void sendNotification(String message) {
    Intent intent = new Intent(this, IntroActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.channel_message_id);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
            .setPriority(NotificationCompat.PRIORITY_MAX).setSmallIcon(R.mipmap.icon)
            .setContentTitle(getString(R.string.app_name)).setContentText(message).setAutoCancel(true)
            .setSound(defaultSoundUri).setContentIntent(pendingIntent);

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

    // Since android Oreo notification channel is needed.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, getString(R.string.channel_message_id),
                NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription(getString(R.string.channel_message));
        notificationManager.createNotificationChannel(channel);
    }//from w w w  .j a va 2 s .c  o  m

    notificationManager.notify(UniqueID.getRandomNumber(1000), notificationBuilder.build());
}

From source file:com.example.petri.myapplication.GCMMessageListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.// w  w w. ja  va 2 s  . c o  m
 */
private void sendNotification(String message, int from_id) {
    Intent intent = new Intent(this, ChatActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("chat_id", from_id);
    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.notification_icon).setContentTitle("GCM Message").setContentText(message)
            .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.mjhram.ttaxi.gcm_client.MyGcmListenerService.java

/**
 * Create and show a simple notification containing the received GCM message.
 *
 * @param message GCM message received.//from   w  ww  .ja va  2  s .  c  o  m
 */
private void sendNotification(String message) {
    Intent intent = new Intent(this, GpsMainActivity.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.circle_green).setContentTitle(getString(R.string.gcmClientGcmMsg))
            .setContentText(message).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.gsma.rcs.ri.sharing.geoloc.GeolocSharingIntentService.java

/**
 * Add geoloc share notification/*from  w  ww .  ja  v a2  s. com*/
 * 
 * @param invitation intent
 */
private void addGeolocSharingInvitationNotification(Intent invitation, ContactId contact) {
    /* Create pending intent */
    Intent intent = new Intent(invitation);
    intent.setClass(this, ReceiveGeolocSharing.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    /*
     * If the PendingIntent has the same operation, action, data, categories, components, and
     * flags it will be replaced. Invitation should be notified individually so we use a random
     * generator to provide a unique request code and reuse it for the notification.
     */
    int uniqueId = Utils.getUniqueIdForPendingIntent();
    PendingIntent contentIntent = PendingIntent.getActivity(this, uniqueId, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String displayName = RcsContactUtil.getInstance(this).getDisplayName(contact);
    String title = getString(R.string.title_recv_geoloc_sharing);

    /* Create notification */
    NotificationCompat.Builder notif = new NotificationCompat.Builder(this);
    notif.setContentIntent(contentIntent);
    notif.setSmallIcon(R.drawable.ri_notif_csh_icon);
    notif.setWhen(System.currentTimeMillis());
    notif.setAutoCancel(true);
    notif.setOnlyAlertOnce(true);
    notif.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    notif.setDefaults(Notification.DEFAULT_VIBRATE);
    notif.setContentTitle(title);
    notif.setContentText(getString(R.string.label_from_args, displayName));

    /* Send notification */
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    notificationManager.notify(uniqueId, notif.build());
}

From source file:com.android.anton.pushnotificationwithgcm.GCMUtil.MyGcmListenerService.java

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());
    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1,
            restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);

    AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 1000,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
}

From source file:com.vanity.mobilevanity.gcm.MyGcmListenerService.java

private void sendNotification(Notify notify) {
    Intent intent = new Intent(this, BeautyTipDetailActivity.class);
    intent.putExtra(BeautyTipDetailActivity.TAG_BEAUTY_TIP_ID,
            notify.getBeautyTipId().getKey().getRaw().getId());
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    int bgColor = MyApplication.getContext().getResources().getColor(R.color.colorMain);

    BitmapDrawable drawable = (BitmapDrawable) MyApplication.getContext().getResources()
            .getDrawable(R.drawable.vanity_logo_big);
    Bitmap appIcon = drawable.getBitmap();

    int smallIconId;

    if (notify.getType().equals("comment"))
        smallIconId = R.drawable.icon_notify_comment;
    else/*w w  w  . j  av a 2 s. c  om*/
        smallIconId = R.drawable.icon_notify_like;

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setLargeIcon(appIcon)
            .setSmallIcon(smallIconId).setColor(bgColor).setTicker("GCM Message").setContentTitle("Vanity")
            .setContentText(notify.getMessage()).setAutoCancel(true).setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

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

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