Skip to content

Commit

Permalink
Android now has us specify mutable PendingIntents.
Browse files Browse the repository at this point in the history
  • Loading branch information
saurik committed Dec 27, 2022
1 parent 509a336 commit 55bea4e
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,21 @@ private Notification.Builder getBuilder() {
}

public void startForeground() {
int immutable = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
immutable |= PendingIntent.FLAG_IMMUTABLE;

Intent startIntent = new Intent(this, MainActivity.class);
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, startIntent, 0);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, startIntent, immutable);
Notification.Builder builder = getBuilder();
builder.setContentTitle(getText(R.string.app));
builder.setContentText(getText(R.string.connected));
builder.setContentIntent(contentIntent);
builder.setOngoing(true);
builder.setSmallIcon(R.drawable.ic_vpn);
Intent i = new Intent();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, immutable);
builder.addAction(R.drawable.ic_vpn, "Stop", pendingIntent);
Notification n = builder.build();
startForeground(1, n);
Expand Down

0 comments on commit 55bea4e

Please sign in to comment.