Java Code Examples for androidx.core.app.TaskStackBuilder#getPendingIntent()

The following examples show how to use androidx.core.app.TaskStackBuilder#getPendingIntent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SecondActivity.java    From AndroidAll with Apache License 2.0 6 votes vote down vote up
public void sendNotificationForBack1() {
    Intent resultIntent = new Intent(this, SecondActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    // 添加返回栈
    stackBuilder.addParentStack(SecondActivity.class);
    // 添加Intent到栈顶
    stackBuilder.addNextIntent(resultIntent);
    // 创建包含返回栈的pendingIntent
    PendingIntent resultPendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentTitle("from SecondActivity");
    builder.setContentText("test notification press back go main activity");
    builder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(2, builder.build());
}
 
Example 2
Source File: StunnelIntentService.java    From SSLSocks with MIT License 5 votes vote down vote up
private void showNotification() {
	NotificationCompat.Builder mBuilder =
			new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
					.setSmallIcon(R.drawable.ic_service_running)
					.setContentTitle(getString(R.string.app_name_full))
					.setContentText(getString(R.string.notification_desc))
					.setCategory(NotificationCompat.CATEGORY_SERVICE)
					.setPriority(NotificationCompat.PRIORITY_DEFAULT)
					.setOngoing(true);
	// Creates an explicit intent for an Activity in your app
	Intent resultIntent = new Intent(this, MainActivity.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(this);
	// 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(
					0,
					PendingIntent.FLAG_UPDATE_CURRENT
			);
	mBuilder.setContentIntent(resultPendingIntent);

	Intent serviceStopIntent = new Intent(this, ServiceStopReceiver.class);
	serviceStopIntent.setAction(ACTION_STOP);
	PendingIntent serviceStopIntentPending = PendingIntent.getBroadcast(this, 1, serviceStopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
	mBuilder.addAction(R.drawable.ic_stop, "Stop", serviceStopIntentPending);

	// Ensure that the service is a foreground service
	startForeground(NOTIFICATION_ID, mBuilder.build());
}
 
Example 3
Source File: PlayerService.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 5 votes vote down vote up
private void buildNotification( NotificationCompat.Action action ) {
	lastNotificationAction = action;

	Intent intent = new Intent( getApplicationContext(), PlayerService.class );
	intent.setAction( ACTION_STOP );
	PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);

	Class toClass = isVod ? VODActivity.class : LiveStreamActivity.class;
	TaskStackBuilder stackBuilder = TaskStackBuilder.create(this).addParentStack(toClass).addNextIntent(mNotificationIntent);
	PendingIntent onClickPendingIntent = stackBuilder.getPendingIntent(
			0,
			PendingIntent.FLAG_UPDATE_CURRENT
	);

	NotificationCompat.Builder noti = new NotificationCompat.Builder( this, getString(R.string.stream_cast_notification_id) )
									.setSmallIcon( R.drawable.ic_notification_icon_refresh )
									.setContentTitle( mChannelInfo.getDisplayName() )
									.setDeleteIntent( pendingIntent )
									.addAction(action)
									.addAction(generateAction(R.drawable.ic_clear_black_36dp, getString(R.string.stop_lower), ACTION_STOP))
									.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
													  .setMediaSession(mediaSession.getSessionToken())
													  .setShowCancelButton(true)
													  .setShowActionsInCompactView(0,1)
									)
									.setShowWhen(false)
									.setAutoCancel(false)
									.setContentIntent(onClickPendingIntent);

	if (mChannelInfo.getLogoImage() != null) {
		noti.setLargeIcon(mChannelInfo.getLogoImage());
	}

	NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
	if (notificationManager == null) return;
	notificationManager.notify( NOTIFICATION_ID, noti.build() );
}
 
Example 4
Source File: NotificationObserver.java    From Kore with Apache License 2.0 5 votes vote down vote up
public NotificationObserver(Service service) {
    this.service = service;

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

    // Create the notification channel
    if (Utils.isOreoOrLater()) {
        buildNotificationChannel();
    }
    nothingPlayingNotification = buildNothingPlayingNotification();
}
 
Example 5
Source File: MageApplication.java    From mage-android with Apache License 2.0 4 votes vote down vote up
public void createNotification() {
	boolean tokenExpired = UserUtility.getInstance(getApplicationContext()).isTokenExpired();

    // Creates an explicit intent for an Activity in your app
	Intent resultIntent = new Intent(this, LoginActivity.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(this);
	// Adds the back stack for the Intent (but not the Intent itself)
	stackBuilder.addParentStack(LoginActivity.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);

	String noticationMsg = tokenExpired ? "Your token has expired, please tap to login." : "You are currently logged into MAGE.";

	Notification accountNotification = new NotificationCompat.Builder(this, MAGE_NOTIFICATION_CHANNEL_ID)
			.setOngoing(true)
			.setSortKey("1")
			.setContentTitle("MAGE")
			.setContentText(noticationMsg)
			.setGroup(MAGE_NOTIFICATION_GROUP)
			.setContentIntent(resultPendingIntent)
			.setSmallIcon(R.drawable.ic_wand_white_50dp)
			.addAction(R.drawable.ic_power_settings_new_white_24dp, "Logout", getLogoutPendingIntent())
			.build();

	NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle()
			.addLine(noticationMsg)
			.setBigContentTitle("MAGE");

	if (isReportingLocation()) {
		style.addLine("MAGE is currently reporting your location.");
	}

	// This summary notification supports "grouping" on versions older that Android.N
	Notification summaryNotification = new NotificationCompat.Builder(this, MAGE_NOTIFICATION_CHANNEL_ID)
			.setGroupSummary(true)
			.setGroup(MAGE_NOTIFICATION_GROUP)
			.setContentTitle("MAGE")
			.setContentText(noticationMsg)
			.setSmallIcon(R.drawable.ic_wand_white_50dp)
			.setStyle(style)
			.setContentIntent(resultPendingIntent)
			.addAction(R.drawable.ic_power_settings_new_white_24dp, "Logout", getLogoutPendingIntent())
			.build();

	NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
	notificationManager.notify(MAGE_ACCOUNT_NOTIFICATION_ID, accountNotification);
	notificationManager.notify(MAGE_SUMMARY_NOTIFICATION_ID, summaryNotification);
}
 
Example 6
Source File: GeofenceTransitionsJobIntentService.java    From location-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Posts a notification in the notification bar when a transition is detected.
 * If the user clicks the notification, control goes to the MainActivity.
 */
private void sendNotification(String notificationDetails) {
    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    // Android O requires a Notification Channel.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.app_name);
        // Create the channel for the notification
        NotificationChannel mChannel =
                new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);

        // Set the Notification Channel for the Notification Manager.
        mNotificationManager.createNotificationChannel(mChannel);
    }

    // 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_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_launcher))
            .setColor(Color.RED)
            .setContentTitle(notificationDetails)
            .setContentText(getString(R.string.geofence_transition_notification_text))
            .setContentIntent(notificationPendingIntent);

    // Set the Channel ID for Android O.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        builder.setChannelId(CHANNEL_ID); // Channel ID
    }

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}
 
Example 7
Source File: Utils.java    From location-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Posts a notification in the notification bar when a transition is detected.
 * If the user clicks the notification, control goes to the MainActivity.
 */
static void sendNotification(Context context, String notificationDetails) {
    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(context, MainActivity.class);

    notificationIntent.putExtra("from_notification", true);

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

    // 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(context);

    // Define the notification settings.
    builder.setSmallIcon(R.mipmap.ic_launcher)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                    R.mipmap.ic_launcher))
            .setColor(Color.RED)
            .setContentTitle("Location update")
            .setContentText(notificationDetails)
            .setContentIntent(notificationPendingIntent);

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

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

    // Android O requires a Notification Channel.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = context.getString(R.string.app_name);
        // Create the channel for the notification
        NotificationChannel mChannel =
                new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);

        // Set the Notification Channel for the Notification Manager.
        mNotificationManager.createNotificationChannel(mChannel);

        // Channel ID
        builder.setChannelId(CHANNEL_ID);
    }

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