feat(notifications): enhance clickable notifications with app launch and action buttons
- Make all notifications clickable to open the app - Use PackageManager.getLaunchIntentForPackage() for reliable app launch - Add 'View Details' action button when URL is available - Maintain existing 'Dismiss' action button for all notifications - Enhanced logging for click intent and action button configuration - Proper Intent flags for NEW_TASK and CLEAR_TOP behavior Features: - Tap notification body: Opens app (with URL data if available) - Tap 'View Details': Opens URL in browser (if URL provided) - Tap 'Dismiss': Dismisses notification and cancels future scheduling Improves user experience with intuitive notification interactions.
This commit is contained in:
@@ -323,22 +323,33 @@ public class DailyNotificationWorker extends Worker {
|
|||||||
builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
|
builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add click action if URL is available
|
// Add click action - always open the app, optionally with URL
|
||||||
|
Intent clickIntent;
|
||||||
if (content.getUrl() != null && !content.getUrl().isEmpty()) {
|
if (content.getUrl() != null && !content.getUrl().isEmpty()) {
|
||||||
Intent clickIntent = new Intent(Intent.ACTION_VIEW);
|
// If URL is provided, open the app and pass the URL as data
|
||||||
|
clickIntent = new Intent(Intent.ACTION_VIEW);
|
||||||
clickIntent.setData(android.net.Uri.parse(content.getUrl()));
|
clickIntent.setData(android.net.Uri.parse(content.getUrl()));
|
||||||
|
clickIntent.setPackage(getApplicationContext().getPackageName());
|
||||||
PendingIntent clickPendingIntent = PendingIntent.getActivity(
|
clickIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
getApplicationContext(),
|
Log.d(TAG, "DN|CLICK_INTENT with_url=" + content.getUrl());
|
||||||
content.getId().hashCode(),
|
} else {
|
||||||
clickIntent,
|
// If no URL, just open the main app
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
clickIntent = getApplicationContext().getPackageManager().getLaunchIntentForPackage(getApplicationContext().getPackageName());
|
||||||
);
|
clickIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
|
Log.d(TAG, "DN|CLICK_INTENT app_only");
|
||||||
builder.setContentIntent(clickPendingIntent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add dismiss action
|
PendingIntent clickPendingIntent = PendingIntent.getActivity(
|
||||||
|
getApplicationContext(),
|
||||||
|
content.getId().hashCode(),
|
||||||
|
clickIntent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
||||||
|
);
|
||||||
|
|
||||||
|
builder.setContentIntent(clickPendingIntent);
|
||||||
|
|
||||||
|
// Add action buttons
|
||||||
|
// 1. Dismiss action
|
||||||
Intent dismissIntent = new Intent(getApplicationContext(), DailyNotificationReceiver.class);
|
Intent dismissIntent = new Intent(getApplicationContext(), DailyNotificationReceiver.class);
|
||||||
dismissIntent.setAction("com.timesafari.daily.DISMISS");
|
dismissIntent.setAction("com.timesafari.daily.DISMISS");
|
||||||
dismissIntent.putExtra("notification_id", content.getId());
|
dismissIntent.putExtra("notification_id", content.getId());
|
||||||
@@ -356,6 +367,30 @@ public class DailyNotificationWorker extends Worker {
|
|||||||
dismissPendingIntent
|
dismissPendingIntent
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// 2. View Details action (if URL is available)
|
||||||
|
if (content.getUrl() != null && !content.getUrl().isEmpty()) {
|
||||||
|
Intent viewDetailsIntent = new Intent(Intent.ACTION_VIEW);
|
||||||
|
viewDetailsIntent.setData(android.net.Uri.parse(content.getUrl()));
|
||||||
|
viewDetailsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||||
|
|
||||||
|
PendingIntent viewDetailsPendingIntent = PendingIntent.getActivity(
|
||||||
|
getApplicationContext(),
|
||||||
|
content.getId().hashCode() + 2000, // Different request code
|
||||||
|
viewDetailsIntent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
||||||
|
);
|
||||||
|
|
||||||
|
builder.addAction(
|
||||||
|
android.R.drawable.ic_menu_info_details,
|
||||||
|
"View Details",
|
||||||
|
viewDetailsPendingIntent
|
||||||
|
);
|
||||||
|
|
||||||
|
Log.d(TAG, "DN|ACTION_BUTTONS added_view_details url=" + content.getUrl());
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "DN|ACTION_BUTTONS dismiss_only");
|
||||||
|
}
|
||||||
|
|
||||||
// Build and display notification
|
// Build and display notification
|
||||||
int notificationId = content.getId().hashCode();
|
int notificationId = content.getId().hashCode();
|
||||||
notificationManager.notify(notificationId, builder.build());
|
notificationManager.notify(notificationId, builder.build());
|
||||||
|
|||||||
Reference in New Issue
Block a user