added permission check for push notification, fixed notification click behavior

This commit is contained in:
nuclearfog 2023-06-01 21:26:07 +02:00
parent 7c923ac199
commit 5dd00e0f54
No known key found for this signature in database
GPG Key ID: 03488A185C476379
3 changed files with 44 additions and 11 deletions

View File

@ -1,9 +1,12 @@
package org.nuclearfog.twidda.notification;
import android.annotation.SuppressLint;
import static android.Manifest.permission.POST_NOTIFICATIONS;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
@ -40,22 +43,21 @@ public class PushNotification {
notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_ID_STR);
settings = GlobalSettings.getInstance(context);
this.context = context;
// if notification is clicked open MainActivity
// todo select notification tab
// Open MainActivity and select notification tab, if notification view is clicked
Intent notificationIntent = new Intent(context.getApplicationContext(), MainActivity.class);
notificationIntent.putExtra(MainActivity.KEY_SELECT_NOTIFICATION, true);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent. FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
notificationBuilder.setContentIntent(resultIntent);
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationBuilder.setContentIntent(resultIntent).setPriority(NotificationCompat.PRIORITY_HIGH).setOnlyAlertOnce(true).setAutoCancel(true);
}
/**
* create push-notification from notifications
*
* @param notifications new notifications
*/
@SuppressLint("MissingPermission")
public void createNotification(Notifications notifications) {
// todo update existing notification and prevent recreating notification
if (!notifications.isEmpty()) {
@ -114,8 +116,10 @@ public class PushNotification {
break;
}
}
notificationBuilder.setContentTitle(title).setContentText(content).setSmallIcon(icon).setOnlyAlertOnce(true).setAutoCancel(true);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || context.checkSelfPermission(POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
notificationBuilder.setContentTitle(title).setContentText(content).setSmallIcon(icon);
notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}
}
}
}

View File

@ -37,6 +37,8 @@ import org.nuclearfog.twidda.ui.views.TabSelector.OnTabSelectedListener;
*/
public class MainActivity extends AppCompatActivity implements ActivityResultCallback<ActivityResult>, OnTabSelectedListener, OnQueryTextListener {
public static final String KEY_SELECT_NOTIFICATION = "main_notification";
private ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), this);
private FragmentAdapter adapter;
@ -94,6 +96,10 @@ public class MainActivity extends AppCompatActivity implements ActivityResultCal
// initialize lists
else if (adapter.isEmpty()) {
setupAdapter();
if (getIntent().getBooleanExtra(KEY_SELECT_NOTIFICATION, false)) {
// select notification page if user clicks on notification
viewPager.setCurrentItem(adapter.getItemCount() - 1, false);
}
}
}

View File

@ -1,9 +1,13 @@
package org.nuclearfog.twidda.ui.activities;
import static android.Manifest.permission.POST_NOTIFICATIONS;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Patterns;
import android.view.Menu;
@ -78,6 +82,8 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
public static final int RETURN_FONT_SCALE_CHANGED = 0x2636;
private static final int REQUEST_PERMISSION_NOTIFICATION = 0x5889;
/**
* total count of all colors defined
*/
@ -107,7 +113,7 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
private View enable_auth_label;
private EditText proxy_address, proxy_port, proxy_user, proxy_pass;
private SwitchButton enable_proxy, enable_auth;
private SwitchButton enable_proxy, enable_auth, enablePush;
private Spinner location_dropdown;
private TextView list_size;
private ViewGroup root;
@ -143,10 +149,10 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
SwitchButton enableLocalTl = findViewById(R.id.settings_local_timeline);
SwitchButton hideSensitive = findViewById(R.id.enable_status_hide_sensitive);
SwitchButton enableStatusIcons = findViewById(R.id.enable_status_indicators);
SwitchButton enablePush = findViewById(R.id.settings_enable_push);
SeekBar listSizeSelector = findViewById(R.id.settings_list_seek);
Spinner fontSelector = findViewById(R.id.spinner_font);
Spinner scaleSelector = findViewById(R.id.spinner_scale);
enablePush = findViewById(R.id.settings_enable_push);
enable_proxy = findViewById(R.id.settings_enable_proxy);
enable_auth = findViewById(R.id.settings_enable_auth);
location_dropdown = findViewById(R.id.spinner_woeid);
@ -309,6 +315,19 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION_NOTIFICATION) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
PushSubscription.subscripe(this);
} else {
enablePush.setChecked(false);
}
}
}
@Override
public void onConfirm(int type) {
// remove account from database
@ -512,7 +531,11 @@ public class SettingsActivity extends AppCompatActivity implements OnClickListen
// enable/disable push notification
else if (c.getId() == R.id.settings_enable_push) {
if (checked) {
PushSubscription.subscripe(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && checkSelfPermission(POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{POST_NOTIFICATIONS}, REQUEST_PERMISSION_NOTIFICATION);
} else {
PushSubscription.subscripe(this);
}
} else {
PushSubscription.unsubscripe(this);
}