Example usage for android.app PendingIntent FLAG_CANCEL_CURRENT

List of usage examples for android.app PendingIntent FLAG_CANCEL_CURRENT

Introduction

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

Prototype

int FLAG_CANCEL_CURRENT

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

Click Source Link

Document

Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one.

Usage

From source file:com.qiscus.sdk.service.QiscusPusherService.java

private void showPushNotification(QiscusComment comment) {
    QiscusCacheManager.getInstance().addMessageNotifItem(comment.getMessage(), comment.getRoomId());

    Intent openIntent = new Intent("com.qiscus.OPEN_COMMENT_PN");
    openIntent.putExtra("data", comment);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, openIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setContentTitle(Qiscus.getChatConfig().getNotificationTitleHandler().getTitle(comment))
            .setContentIntent(pendingIntent)
            .setContentText(isAttachment(comment.getMessage()) ? fileMessage : comment.getMessage())
            .setTicker(isAttachment(comment.getMessage()) ? fileMessage : comment.getMessage())
            .setSmallIcon(Qiscus.getChatConfig().getNotificationSmallIcon())
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    Qiscus.getChatConfig().getNotificationBigIcon()))
            .setGroupSummary(true).setGroup("CHAT_NOTIF_" + comment.getRoomId()).setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL);

    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    List<String> notifItems = QiscusCacheManager.getInstance().getMessageNotifItems(comment.getRoomId());
    for (String message : notifItems) {
        if (isAttachment(message)) {
            inboxStyle.addLine(fileMessage);
        } else {/*  w  w  w  .ja  va  2  s.c o  m*/
            inboxStyle.addLine(message);
        }
    }
    inboxStyle.setSummaryText(notifItems.size() + " new message");
    notificationBuilder.setStyle(inboxStyle);

    NotificationManagerCompat.from(this).notify(comment.getRoomId(), notificationBuilder.build());
}

From source file:eu.inmite.apps.smsjizdenka.util.NotificationUtil.java

/**
 * Verification if tickets is bought 1 minute after another.
 *//*from  ww w.j  a  v a2 s  .  co  m*/
public static void notifyVerification(Context c, City city) {
    Intent i = new Intent(c, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.putExtra(MainActivity.EXTRA_REALLY_BUY_CITY_ID, city.id);
    PendingIntent contentIntent = PendingIntent.getActivity(c, NOTIFICATION_VERIFY, i,
            PendingIntent.FLAG_CANCEL_CURRENT);
    String title = c.getString(R.string.cities_ticket_bought_again_verification);
    String text = c.getString(R.string.cities_ticket_bought_again_action);
    fireNotification(c, NOTIFICATION_VERIFY, contentIntent, title, text, null, null, title,
            R.drawable.notification_small_warning, R.drawable.notification_big_warning, null, false);
}

From source file:de.appplant.cordova.plugin.notification.Builder.java

/**
 * Set intent to handle the delete event. Will clean up some persisted
 * preferences./*  www .j  a va 2  s.  c o  m*/
 *
 * @param builder
 *      Local notification builder instance
 */
private void applyDeleteReceiver(NotificationCompat.Builder builder) {

    if (clearReceiver == null)
        return;

    Intent deleteIntent = new Intent(context, clearReceiver).setAction(options.getId()).putExtra(Options.EXTRA,
            options.toString());

    PendingIntent dpi = PendingIntent.getBroadcast(context, 0, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    builder.setDeleteIntent(dpi);
}

From source file:com.example.android.lnotifications.HeadsUpNotificationFragment.java

/**
 * Creates a new notification depending on the argument.
 *
 * @param makeHeadsUpNotification A boolean value to indicating whether a notification will be
 *                                created as a heads-up notification or not.
 *                                <ul>
 *                                <li>true : Creates a heads-up notification.</li>
 *                                <li>false : Creates a non-heads-up notification.</li>
 *                                </ul>
 *
 * @return A Notification instance./*from w ww . ja va2s.c om*/
 */
private Notification createNotification(boolean makeHeadsUpNotification) {
    Notification.Builder notificationBuilder = new Notification.Builder(getActivity())
            .setSmallIcon(R.drawable.ic_launcher_notification).setPriority(Notification.PRIORITY_DEFAULT)
            .setCategory(Notification.CATEGORY_MESSAGE).setContentTitle("Sample Notification")
            .setContentText("This is a normal notification.");
    if (makeHeadsUpNotification) {
        Intent push = new Intent();
        push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        push.setClass(getActivity(), LNotificationActivity.class);

        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(getActivity(), 0, push,
                PendingIntent.FLAG_CANCEL_CURRENT);
        notificationBuilder.setContentText("Heads-Up Notification on Android L or above.")
                .setFullScreenIntent(fullScreenPendingIntent, true);
    }
    return notificationBuilder.build();
}

From source file:com.echopf.ECHOFcmListenerService.java

/**
 * Sets notification layouts./*from  w  w w .  j av  a 2s .  c  o  m*/
 * @param data 
 * @return NotificationCompat.Builder
 */
public NotificationCompat.Builder getNotificationBuilder(Bundle data) {
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setAutoCancel(true);

    // get ApplicationInfo
    ApplicationInfo appInfo = null;
    String appName = null;
    try {
        appInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
        appName = getPackageManager()
                .getApplicationLabel(getPackageManager().getApplicationInfo(getPackageName(), 0)).toString();
    } catch (NameNotFoundException e) {
        throw new RuntimeException(e);
    }

    // set title
    String title = (data.getString("title") != null) ? data.getString("title") : appName;
    notificationBuilder.setContentTitle(title);

    // set message
    if (data.getString("message") != null)
        notificationBuilder.setContentText(data.getString("message"));

    // set icon
    int manifestIco = appInfo.metaData.getInt(NOTIFICATION_ICON_KEY);
    int icon = (manifestIco != 0) ? manifestIco : appInfo.icon;
    notificationBuilder.setSmallIcon(icon);

    // set sound
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    notificationBuilder.setSound(defaultSoundUri);

    // set content intent
    String activity = appInfo.metaData.getString(PUSH_OPEN_ACTIVITY_KEY);
    if (activity != null) {

        try {
            Intent intent = new Intent(this, Class.forName(activity)).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
                    .setComponent(new ComponentName(appInfo.packageName, activity)).putExtras(data);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_CANCEL_CURRENT);
            notificationBuilder.setContentIntent(pendingIntent);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

    return notificationBuilder;
}

From source file:org.thoughtcrime.securesms.jobs.FcmRefreshJob.java

private void notifyFcmFailure() {
    Intent intent = new Intent(context, PlayServicesProblemActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1122, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannels.FAILURES);

    builder.setSmallIcon(R.drawable.icon_notification);
    builder.setLargeIcon(//from  w  ww  . jav  a  2  s  . c  om
            BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_action_warning_red));
    builder.setContentTitle(context.getString(R.string.GcmRefreshJob_Permanent_Signal_communication_failure));
    builder.setContentText(
            context.getString(R.string.GcmRefreshJob_Signal_was_unable_to_register_with_Google_Play_Services));
    builder.setTicker(context.getString(R.string.GcmRefreshJob_Permanent_Signal_communication_failure));
    builder.setVibrate(new long[] { 0, 1000 });
    builder.setContentIntent(pendingIntent);

    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(12, builder.build());
}

From source file:com.sonetel.plugins.sonetelcallthru.CallThru.java

@Override
public void onReceive(final Context context, Intent intent) {
    if (SipManager.ACTION_GET_PHONE_HANDLERS.equals(intent.getAction())) {
        // Retrieve and cache infos from the phone app 
        if (!sPhoneAppInfoLoaded) {
            Resources r = context.getResources();
            BitmapDrawable drawable = (BitmapDrawable) r.getDrawable(R.drawable.ic_wizard_sonetel);
            sPhoneAppBmp = drawable.getBitmap();

            sPhoneAppInfoLoaded = true;/*from   w  ww .j a  va  2 s .  co m*/
        }

        Bundle results = getResultExtras(true);
        //if(pendingIntent != null) {
        //   results.putParcelable(CallHandlerPlugin.EXTRA_REMOTE_INTENT_TOKEN, pendingIntent);
        //}
        results.putString(Intent.EXTRA_TITLE, "Call thru");// context.getResources().getString(R.string.use_pstn));
        if (sPhoneAppBmp != null) {
            results.putParcelable(Intent.EXTRA_SHORTCUT_ICON, sPhoneAppBmp);
        }
        if (intent.getStringExtra(Intent.EXTRA_TEXT) == null)
            return;

        //final PendingIntent pendingIntent = null;
        final String callthrunum = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        final String dialledNum = intent.getStringExtra(Intent.EXTRA_TEXT);
        // We must handle that clean way cause when call just to 
        // get the row in account list expect this to reply correctly
        if (callthrunum != null && PhoneCapabilityTester.isPhone(context)) {

            if (!callthrunum.equalsIgnoreCase("")) {
                DBAdapter dbAdapt = new DBAdapter(context);

                // Build pending intent

                SipAccount = dbAdapt.getAccount(1);

                if (SipAccount != null) {

                    Intent i = new Intent(Intent.ACTION_CALL);
                    i.setData(Uri.fromParts("tel", callthrunum, null));
                    final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i,
                            PendingIntent.FLAG_CANCEL_CURRENT);

                    NetWorkThread = new Thread() {
                        public void run() {
                            SendMsgToNetWork(dialledNum, context, pendingIntent);
                        };
                    };

                    NetWorkThread.start();

                }

                if (!dialledNum.equalsIgnoreCase("")) {
                    SipCallSessionImpl callInfo = new SipCallSessionImpl();

                    callInfo.setRemoteContact(dialledNum);
                    callInfo.setIncoming(false);
                    callInfo.callStart = System.currentTimeMillis();
                    callInfo.setAccId(3);
                    //callInfo.setLastStatusCode(pjsua.PJ_SUCCESS);

                    ContentValues cv = CallLogHelper.logValuesForCall(context, callInfo, callInfo.callStart);
                    context.getContentResolver().insert(SipManager.CALLLOG_URI, cv);

                }
            } else {
                Location_Finder LocFinder = new Location_Finder(context);
                if (LocFinder.getContryName().startsWith("your"))
                    Toast.makeText(context,
                            "Country of your current location unknown. Call thru not available.",
                            Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(context,
                            "No Call thru access number available in " + LocFinder.getContryName(),
                            Toast.LENGTH_LONG).show();
                //Toast.makeText(context, "No Call thru access number available in ", Toast.LENGTH_LONG).show();
            }
        }

        // This will exclude next time tel:xxx is raised from csipsimple treatment which is wanted
        results.putString(Intent.EXTRA_PHONE_NUMBER, callthrunum);

    }

}

From source file:jieehd.villain.updater.VillainUpdater.java

public void setRepeatingAlarm() {
    AlarmManager am;// w  w  w .j  a v a2s .c  o  m
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, checkInBackground.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (3 * AlarmManager.INTERVAL_DAY),
            pendingIntent);
}

From source file:org.c99.wear_imessage.GCMIntentService.java

public static void notify(Context context, int notification_id, JSONArray msgs, Intent intent,
        boolean updateOnly) throws JSONException {
    String lastMsg = intent.getStringExtra("msg");
    Spanned msg;//  w ww.  j  a v a2s  .c  om
    String name = intent.getStringExtra("name");
    if (name.contains(" "))
        name = name.substring(0, name.indexOf(" "));

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < msgs.length(); i++) {
        if (sb.length() > 0)
            sb.append("<br/><br/>");
        String body;
        String from = name;

        try {
            JSONObject o = msgs.getJSONObject(i);
            body = o.getString("msg");
            if (o.has("type")) {
                if (o.getString("type").equals("file") || o.getString("type").equals("sent_file"))
                    body = "[File: " + o.getString("msg") + "]";

                if (o.getString("type").equals("sent") || o.getString("type").equals("sent_file"))
                    from = "Me";
                else
                    lastMsg = body;
            } else {
                from = name;
                lastMsg = body;
            }
        } catch (JSONException e1) {
            from = name;
            body = msgs.getString(i);
            lastMsg = body;
        }

        sb.append("<b>").append(Html.escapeHtml(from)).append(":</b> ").append(Html.escapeHtml(body));
    }

    msg = Html.fromHtml(sb.toString());
    int msg_count = msgs.length();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setAutoCancel(true);

    if (name.length() > 0) {
        builder.setSmallIcon(R.drawable.ic_notification).setContentTitle(intent.getStringExtra("name"))
                .setContentText(lastMsg).setNumber(msg_count);
    }

    if (updateOnly) {
        builder.setVibrate(null);
    } else {
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);
        builder.setTicker(Html.fromHtml("<b>" + Html.escapeHtml(intent.getStringExtra("name")) + ":</b> "
                + Html.escapeHtml(intent.getStringExtra("msg")))).setDefaults(Notification.DEFAULT_ALL);
    }

    NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
    style.bigText(lastMsg);
    builder.setStyle(style);

    NotificationCompat.WearableExtender extender = new NotificationCompat.WearableExtender();

    if (ENABLE_REPLIES) {
        Intent replyIntent = new Intent(RemoteInputService.ACTION_REPLY);
        replyIntent
                .setComponent(new ComponentName(context.getPackageName(), RemoteInputService.class.getName()));
        replyIntent.putExtras(intent.getExtras());
        replyIntent.putExtra("notification_id", notification_id);
        PendingIntent replyPendingIntent = PendingIntent.getService(context, notification_id + 1, replyIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        extender.addAction(
                new NotificationCompat.Action.Builder(R.drawable.ic_reply, "Reply", replyPendingIntent)
                        .addRemoteInput(new RemoteInput.Builder("extra_reply").setLabel("Reply").build())
                        .build());

        Intent launchIntent = new Intent(context, QuickReplyActivity.class);
        launchIntent.putExtras(intent.getExtras());
        launchIntent.putExtra("notification_id", notification_id);
        builder.setContentIntent(PendingIntent.getActivity(context, notification_id + 2, launchIntent,
                PendingIntent.FLAG_CANCEL_CURRENT));
        builder.addAction(R.drawable.ic_action_reply, "Reply",
                PendingIntent.getActivity(context, notification_id + 3, launchIntent,
                        PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_CANCEL_CURRENT));
    }

    if (msg_count > 1)
        extender.addPage(new NotificationCompat.Builder(context).setContentText(msg)
                .extend(new NotificationCompat.WearableExtender().setStartScrollBottom(true)).build());

    Cursor c = null;
    if (intent.getStringExtra("handle").contains("@"))
        c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
                new String[] { ContactsContract.Data.CONTACT_ID },
                ContactsContract.CommonDataKinds.Email.ADDRESS + "=?" + " AND " + ContactsContract.Data.MIMETYPE
                        + "=?",
                new String[] { intent.getStringExtra("handle"),
                        ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE },
                null);

    if (c != null && c.moveToFirst()) {
        long contactId = c.getLong(0);
        c.close();

        Bitmap b = BitmapFactory.decodeStream(
                ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId), true));
        if (b != null) {
            builder.setLargeIcon(b);
            extender.setBackground(b);
        }
    }

    NotificationManagerCompat m = NotificationManagerCompat.from(context);
    m.notify(notification_id, builder.extend(extender).build());
}

From source file:de.appplant.cordova.plugin.notification.Notification.java

/**
 * Schedule the local notification./*from w  w w  .  j  ava 2 s  . c o  m*/
 */
public void schedule() {
    long triggerTime = getNextTriggerTime();

    persist();

    // Intent gets called when the Notification gets fired
    Intent intent = new Intent(context, receiver).setAction(options.getId()).putExtra(Options.EXTRA,
            options.toString());

    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    getAlarmMgr().set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
}