This might not be possible. It relies on the OS.
Possible reasons for this is notifications are for the user to click and open the app usually. And this might go against apple app guidelines too to prevent the app from opening.
However, you can try these:
This is an example of a foreground service with the notification.
public class UploadService extends IntentService{
private NotificationCompat.Builder mBuilder;
public UploadService() {
super("UploadService");
}
@Override
protected void onHandleIntent(Intent intent) {
Intent deleteIntent = new Intent(this, CancelUploadReceiver.class);
PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//building the notification
mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_menu_upload)
.setContentTitle("Uploading Media...")
.setTicker("Starting uploads")
.addAction(android.R.drawable.ic_menu_close_clear_cancel, "Cancel Upload", pendingIntentCancel);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
mBuilder.setProgress(100, 0, true);
startForeground(12345, mBuilder.build());
for(int i=0;i<10;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
You need to register the CancelUploadReceiver in the manifest file.
<receiver android:name=".CancelUploadReceiver"/>
And when the “Cancel Upload” is tapped it will receive the broadcast. Then we can simply stop the service.
public class CancelUploadReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent();
service.setComponent(new ComponentName(context,UploadService.class));
context.stopService(service);
}
}
Another option might be to create a Custom Interface that implements the UINotificationContentExtension protocol.
Related: