I am trying to create a service in my android app. This service further starts a foreground service. I am trying to make sure that when i click this notification of this foreground service I am taken to channel settings of this notfication(where user can easily disable the notfications for this channel).But this isn't happening. Instead when I click the settings gets crashed. Where am I going wrong? This is the code which I have used:
public class AlarmHandlerService extends Service {
public NotificationCompat.Builder createNotification(String title, String content, String channel_id, int priority) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channel_id)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(content)
.setPriority(priority);
return builder;
}
@Override
public void onCreate() {
super.onCreate();
Intent i = new Intent(Settings
.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
.putExtra(Settings.EXTRA_APP_PACKAGE, AlarmHandlerService.class)
.putExtra(Settings.EXTRA_CHANNEL_ID, "foreground_services")
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
1,
i,
PendingIntent.FLAG_UPDATE_CURRENT
);
startForeground(1, createNotification("Foreground Service", "Click here to disable this foreground service", "foreground_services", NotificationCompat.PRIORITY_DEFAULT).setContentIntent(pendingIntent).build());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
EDIT: Figured out the solution...as correctly pointed by @snachmsm I had set up my intent in the wrong way...This should be the correct code for the intent..
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, "foreground_services");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
1,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
you are creating
IntentwithService(assuming by name ofAlarmHandlerService.class), then you are using it for creatingPendingIntentwithgetActivitymethod. it should begetServicemethodbtw. this is called "notification trampoline" (running non-UI
Contextafter notification click) and is forbidden on newest (currenlty) Anroid 12. some article about