Example usage for android.support.v4.app TaskStackBuilder getPendingIntent

List of usage examples for android.support.v4.app TaskStackBuilder getPendingIntent

Introduction

In this page you can find the example usage for android.support.v4.app TaskStackBuilder getPendingIntent.

Prototype

public PendingIntent getPendingIntent(int requestCode, int flags) 

Source Link

Document

Obtain a PendingIntent for launching the task constructed by this builder so far.

Usage

From source file:com.chen.mail.utils.NotificationActionUtils.java

/**
 * Creates a {@link PendingIntent} for the specified notification action.
 *//*from w ww  .j  a v a 2 s  .c o  m*/
private static PendingIntent getNotificationActionPendingIntent(final Context context, final Account account,
        final Conversation conversation, final Message message, final Folder folder,
        final Intent notificationIntent, final NotificationActionType action, final int notificationId,
        final long when) {
    final Uri messageUri = message.uri;

    final NotificationAction notificationAction = new NotificationAction(action, account, conversation, message,
            folder, conversation.id, message.serverId, message.id, when);

    switch (action) {
    case REPLY: {
        // Build a task stack that forces the conversation view on the stack before the
        // reply activity.
        final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);

        final Intent intent = createReplyIntent(context, account, messageUri, false);
        intent.setPackage(context.getPackageName());
        intent.putExtra(ComposeActivity.EXTRA_NOTIFICATION_FOLDER, folder);
        // To make sure that the reply intents one notification don't clobber over
        // intents for other notification, force a data uri on the intent
        final Uri notificationUri = Uri.parse("mailfrom://mail/account/" + "reply/" + notificationId);
        intent.setData(notificationUri);

        taskStackBuilder.addNextIntent(notificationIntent).addNextIntent(intent);

        return taskStackBuilder.getPendingIntent(notificationId, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    case REPLY_ALL: {
        // Build a task stack that forces the conversation view on the stack before the
        // reply activity.
        final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);

        final Intent intent = createReplyIntent(context, account, messageUri, true);
        intent.setPackage(context.getPackageName());
        intent.putExtra(ComposeActivity.EXTRA_NOTIFICATION_FOLDER, folder);
        // To make sure that the reply intents one notification don't clobber over
        // intents for other notification, force a data uri on the intent
        final Uri notificationUri = Uri.parse("mailfrom://mail/account/" + "replyall/" + notificationId);
        intent.setData(notificationUri);

        taskStackBuilder.addNextIntent(notificationIntent).addNextIntent(intent);

        return taskStackBuilder.getPendingIntent(notificationId, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    case ARCHIVE_REMOVE_LABEL: {
        final String intentAction = NotificationActionIntentService.ACTION_ARCHIVE_REMOVE_LABEL;

        final Intent intent = new Intent(intentAction);
        intent.setPackage(context.getPackageName());
        putNotificationActionExtra(intent, notificationAction);

        return PendingIntent.getService(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    case DELETE: {
        final String intentAction = NotificationActionIntentService.ACTION_DELETE;

        final Intent intent = new Intent(intentAction);
        intent.setPackage(context.getPackageName());
        putNotificationActionExtra(intent, notificationAction);

        return PendingIntent.getService(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
    }

    throw new IllegalArgumentException("Invalid NotificationActionType");
}

From source file:org.xbmc.kore.service.NotificationObserver.java

public NotificationObserver(Context context) {
    this.mContext = context;

    // Create the intent to start the remote when the user taps the notification
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
    stackBuilder.addParentStack(RemoteActivity.class);
    stackBuilder.addNextIntent(new Intent(mContext, RemoteActivity.class));
    mRemoteStartPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:com.z3r0byte.magistify.Services.NewGradeService.java

private void GradeChecker() {
    TimerTask refreshSession = new TimerTask() {
        @Override//from w w  w  . j av a2 s . c o  m
        public void run() {
            Magister magister = GlobalAccount.MAGISTER;
            if (magister == null || magister.isExpired()) {
                Log.e(TAG, "run: Invalid magister");
                return;
            }

            NewGradesDB gradesdb = new NewGradesDB(getApplicationContext());

            GradeHandler gradeHandler = new GradeHandler(magister);
            Grade[] gradeArray;
            List<Grade> gradeList = new ArrayList<Grade>();
            try {
                gradeArray = gradeHandler.getRecentGrades();
                gradesdb.addGrades(gradeArray);
                Collections.reverse(Arrays.asList(gradeArray));

                //For testing purposes:
                /*Grade sampleGrade = new Grade();
                sampleGrade.isSufficient = false;
                sampleGrade.grade = "2.3";
                sampleGrade.subject = new SubSubject();
                sampleGrade.subject.name = "Latijn";
                        
                Grade sampleGrade2 = new Grade();
                sampleGrade2.isSufficient = true;
                sampleGrade2.grade = "6.5";
                sampleGrade2.subject = new SubSubject();
                sampleGrade2.subject.name = "Nederlands";
                        
                gradeArray = new Grade[2];
                gradeArray[0] = sampleGrade;
                gradeArray[1] = sampleGrade2;*/

                for (Grade grade : gradeArray) {
                    if (!gradesdb.hasBeenSeen(grade, false)
                            && (grade.isSufficient || !configUtil.getBoolean("pass_grades_only"))) {
                        gradeList.add(grade);
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
            String GradesNotification = new Gson().toJson(gradeList);
            if (gradeList != null && gradeList.size() > 0
                    && !configUtil.getString("lastGradesNotification").equals(GradesNotification)) {

                Log.d(TAG, "run: Some grades to show: " + gradeList.size());

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext());
                mBuilder.setSmallIcon(R.drawable.ic_grade_notification);

                if (gradeList.size() == 1) {
                    Grade grade = gradeList.get(0);
                    mBuilder.setContentTitle("Nieuw cijfer voor " + grade.subject.name);
                    //mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder).bigText())
                    mBuilder.setContentText("Een " + grade.grade);
                } else {
                    String content = "";
                    for (Grade grade : gradeList) {
                        String string = grade.subject.name + ", een " + grade.grade;
                        if (content.length() > 1) {
                            content = content + "\n" + string;
                        } else {
                            content = string;
                        }
                    }
                    mBuilder.setContentTitle("Nieuwe cijfers voor:");
                    mBuilder.setStyle(new NotificationCompat.BigTextStyle(mBuilder).bigText(content));
                    mBuilder.setContentText(content);
                }
                mBuilder.setAutoCancel(true);
                mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
                mBuilder.setDefaults(Notification.DEFAULT_ALL);

                Intent resultIntent = new Intent(getApplicationContext(), NewGradeActivity.class);
                TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
                stackBuilder.addParentStack(NewGradeActivity.class);
                stackBuilder.addNextIntent(resultIntent);
                PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT);
                mBuilder.setContentIntent(resultPendingIntent);

                NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(9992, mBuilder.build());

                configUtil.setString("lastGradesNotification", GradesNotification);
            } else {
                Log.w(TAG, "run: No grades!");
            }
        }
    };
    timer.schedule(refreshSession, 6000, 10 * 1000);
}

From source file:edu.ucsb.cs.cs190i.evgeny.evgenygeofencing.GeofenceTransitionsIntentService.java

private void sendNotification(String notificationDetails) {
    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);

    // Construct a task stack.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Add the main Activity to the task stack as the parent.
    stackBuilder.addParentStack(MainActivity.class);

    // Push the content Intent onto the stack.
    stackBuilder.addNextIntent(notificationIntent);

    // Get a PendingIntent containing the entire back stack.
    PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Get a notification builder that's compatible with platform versions >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    // Define the notification settings.
    builder.setSmallIcon(R.drawable.ic_globe_white)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_globe_blue))
            .setColor(Color.RED).setContentTitle(notificationDetails)
            .setContentText(getString(R.string.geofence_transition_notification_text))
            .setContentIntent(notificationPendingIntent).setAutoCancel(true); // Dismiss notification once the user touches it.

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

From source file:de.qspool.clementineremote.backend.downloader.DownloadManager.java

private PendingIntent buildNotificationIntent() {
    Intent intent = new Intent(mContext, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    intent.putExtra(ClementineMediaSessionNotification.EXTRA_NOTIFICATION_ID, NOTIFICATION_ID_DOWNLOADS);

    // Create a TaskStack, so the app navigates correctly backwards
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(intent);//from  w ww. j  av  a  2  s.co  m
    return stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:org.qeo.deviceregistration.ui.NotificationHelper.java

private void remoteDeviceFoundNotification(Context context, Intent intent) {
    LOG.fine("(un)publishing notification for remote device");
    List<RegistrationRequest> unregistered = new ArrayList<RegistrationRequest>();
    for (RegistrationRequest req : RemoteDeviceRegistration.getInstance().getUnregisteredDevices()) {
        RegistrationStatusCode status = UnRegisteredDeviceModel.getRegistrationStatus(req.registrationStatus);
        if (status == RegistrationStatusCode.UNREGISTERED) {
            // only add unregistered devices
            unregistered.add(req);/*from ww  w .j a va2 s.  c o m*/
        }
    }

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    if (unregistered.isEmpty()) {
        // no more devices in unregistered state, remove notification
        notificationManager.cancel(ServiceApplication.NOTIFICATION_UNREGISTERED_DEVICE_FOUND);
        return;
    }

    // need to create the notification

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.drawable.ic_stat_qeo);
    builder.setContentTitle(context.getString(R.string.unregistered_device_notification_title));
    builder.setContentText(context.getString(R.string.unregistered_device_notification_subtitle));
    builder.setAutoCancel(true);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(context, ManagementActivity.class);
    resultIntent.putExtra(ManagementActivity.INTENT_EXTRA_START_TAB, ManagementActivity.TAB_DEVICES);
    if (unregistered.size() == 1) {
        // only 1 device
        UnRegisteredDeviceModel device = new UnRegisteredDeviceModel(unregistered.get(0));
        resultIntent.putExtra(UnRegisteredDeviceListFragment.INTENT_EXTRA_DEVICE_TO_REGISTER, device);
    }

    // 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(MainActivity.class);
    // Adds the Intent that starts the Activity to the top of the stack
    stackBuilder.addNextIntent(resultIntent);

    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
            ServiceApplication.REQUEST_CODE_UNREGISTERED_DEVICE_FOUND, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(resultPendingIntent);

    // mId allows you to update the notification later on.
    notificationManager.notify(ServiceApplication.NOTIFICATION_UNREGISTERED_DEVICE_FOUND, builder.build());

}

From source file:net.kourlas.voipms_sms.notifications.Notifications.java

/**
 * Shows a notification with the specified details.
 *
 * @param contact   The contact that the notification is from.
 * @param shortText The short form of the message text.
 * @param longText  The long form of the message text.
 *///from w  w  w  .j a  v a2  s.  co m
private void showNotification(String contact, String shortText, String longText) {

    String title = Utils.getContactName(applicationContext, contact);
    if (title == null) {
        title = Utils.getFormattedPhoneNumber(contact);
    }
    NotificationCompat.Builder notification = new NotificationCompat.Builder(applicationContext);
    notification.setContentTitle(title);
    notification.setContentText(shortText);
    notification.setSmallIcon(R.drawable.ic_chat_white_24dp);
    notification.setPriority(Notification.PRIORITY_HIGH);
    String notificationSound = Preferences.getInstance(applicationContext).getNotificationSound();
    if (!notificationSound.equals("")) {
        notification.setSound(Uri.parse(Preferences.getInstance(applicationContext).getNotificationSound()));
    }
    String num = Utils.getFormattedPhoneNumber(contact);
    Log.v("prior", num);

    SharedPreferences sharedPreferences = applicationContext.getSharedPreferences("ledData",
            Context.MODE_PRIVATE);
    String cNm = sharedPreferences.getString(title, "3000");
    SharedPreferences sharedPref = applicationContext.getSharedPreferences("color", Context.MODE_PRIVATE);
    int clr = sharedPref.getInt(title, 0xFF02ffff);
    Log.v("saved rate of ", cNm + " for " + title);
    int ledSpeed = 3000;
    int color = 0xFF02ffff;

    if (cNm.equals("500") || cNm.equals("3000")) {
        ledSpeed = Integer.parseInt(cNm);
        color = clr;
    }

    notification.setLights(color, ledSpeed, ledSpeed);

    if (Preferences.getInstance(applicationContext).getNotificationVibrateEnabled()) {
        notification.setVibrate(new long[] { 0, 250, 250, 250 });
    } else {
        notification.setVibrate(new long[] { 0 });
    }
    notification.setColor(0xFF546e7a);
    notification.setAutoCancel(true);
    notification.setStyle(new NotificationCompat.BigTextStyle().bigText(longText));

    Bitmap largeIconBitmap;
    try {
        largeIconBitmap = MediaStore.Images.Media.getBitmap(applicationContext.getContentResolver(),
                Uri.parse(Utils.getContactPhotoUri(applicationContext, contact)));
        largeIconBitmap = Bitmap.createScaledBitmap(largeIconBitmap, 256, 256, false);
        largeIconBitmap = Utils.applyCircularMask(largeIconBitmap);
        notification.setLargeIcon(largeIconBitmap);
    } catch (Exception ignored) {
        // Do nothing.
    }

    Intent intent = new Intent(applicationContext, ConversationActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra(applicationContext.getString(R.string.conversation_extra_contact), contact);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(applicationContext);
    stackBuilder.addParentStack(ConversationActivity.class);
    stackBuilder.addNextIntent(intent);
    notification.setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT));

    Intent replyIntent = new Intent(applicationContext, ConversationQuickReplyActivity.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
    } else {
        //noinspection deprecation
        replyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    }
    replyIntent.putExtra(applicationContext.getString(R.string.conversation_extra_contact), contact);
    PendingIntent replyPendingIntent = PendingIntent.getActivity(applicationContext, 0, replyIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Action.Builder replyAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_reply_white_24dp, applicationContext.getString(R.string.notifications_button_reply),
            replyPendingIntent);
    notification.addAction(replyAction.build());

    Intent markAsReadIntent = new Intent(applicationContext, MarkAsReadReceiver.class);
    markAsReadIntent.putExtra(applicationContext.getString(R.string.conversation_extra_contact), contact);
    PendingIntent markAsReadPendingIntent = PendingIntent.getBroadcast(applicationContext, 0, markAsReadIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Action.Builder markAsReadAction = new NotificationCompat.Action.Builder(
            R.drawable.ic_drafts_white_24dp,
            applicationContext.getString(R.string.notifications_button_mark_read), markAsReadPendingIntent);
    notification.addAction(markAsReadAction.build());

    int id;
    if (notificationIds.get(contact) != null) {
        id = notificationIds.get(contact);
    } else {
        id = notificationIdCount++;
        notificationIds.put(contact, id);
    }
    NotificationManager notificationManager = (NotificationManager) applicationContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(id, notification.build());
}

From source file:com.snippet.app.NotificationActivity.java

public void onButton2(final View v) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), android.R.drawable.sym_def_app_icon))
            .setSmallIcon(android.R.drawable.stat_notify_chat).setTicker("Silent notification")
            .setContentTitle("This is a silent notification")
            .setContentText("ContentText: Hello world! The quick brown fox jumps over the lazy dog.")
            .setSubText("SubText").setContentInfo("10").setAutoCancel(true);
    Intent resultIntent = new Intent(this, NotificationResultActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(NotificationResultActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);
    notificationManager.notify(2, builder.build());
}

From source file:com.snippet.app.NotificationActivity.java

public void onButton1(final View v) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setDefaults(Notification.DEFAULT_ALL)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), android.R.drawable.sym_def_app_icon))
            .setSmallIcon(android.R.drawable.stat_notify_chat).setTicker("Ticker")
            .setContentTitle("ContentTitle")
            .setContentText("ContentText: Hello world! The quick brown fox jumps over the lazy dog.")
            .setSubText("SubText").setContentInfo("10").setAutoCancel(true);
    Intent resultIntent = new Intent(this, NotificationResultActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(NotificationResultActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);
    notificationManager.notify(1, builder.build());
}

From source file:com.snippet.app.NotificationActivity.java

public void onButton3(final View v) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setDefaults(Notification.DEFAULT_ALL)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .setSmallIcon(R.drawable.ic_stat_sample).setTicker("Custom icons")
            .setContentTitle("This is a custom icon notification")
            .setContentText("ContentText: Hello world! The quick brown fox jumps over the lazy dog.")
            .setSubText("SubText").setContentInfo("10").setAutoCancel(true);
    Intent resultIntent = new Intent(this, NotificationResultActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(NotificationResultActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    NotificationManager notificationManager = (NotificationManager) getSystemService(
            Context.NOTIFICATION_SERVICE);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);
    notificationManager.notify(3, builder.build());
}