I'm building chat application and I'm using pusher service for this. as I need my application received messages when user close the application I used JobService and it works correctly when user return back from application but when user remove application from recent app jobService doesn't receive messages at all.
this is my code
PusherService.java
public class PusherService extends JobService {
private Pusher pusher;
@Override
public boolean onStartJob(JobParameters params) {
Context context = this;
System.out.println("Started");
PusherOptions options = new PusherOptions();
options.setCluster(Constants.CLUSTER);
options.buildUrl(Constants.KEY);
pusher = new Pusher(Constants.KEY, options);
pusher.connect(new ConnectionEventListener() {
@Override
public void onConnectionStateChange(ConnectionStateChange change) {
Log.i("Pusher", "State changed from " + change.getPreviousState() +
" to " + change.getCurrentState());
}
@Override
public void onError(String message, String code, Exception e) {
}
}, ConnectionState.ALL);
int userID = 21;
Channel channel = pusher.subscribe(Constants.CHANNEL + userID);
channel.bind(Constants.EVENT_NAME, new SubscriptionEventListener() {
@Override
public void onEvent(PusherEvent event) {
Log.i("New Message received", "Message recieved");
try {
JSONObject jsonObject = new JSONObject(event.getData());
JSONObject messageObject = jsonObject.getJSONObject("message");
int user_id = messageObject.getInt("sender id");
if (user_id == userID) {
System.out.println("sent message");
} else {
System.out.println("received message");
}
jobFinished(params, true);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
pusher.connect();
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
if (pusher != null) {
pusher.disconnect();
}
System.out.println("Stopped");
return true;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private APIService service;
private final String TAG = "API Message";
private int receiverID = 9;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
startJob();
}
private void startJob() {
// Create a JobInfo specifying your JobService and its constraints
ComponentName componentName = new ComponentName(this, PusherService.class);
JobInfo jobInfo = new JobInfo.Builder(Constants.JOB_ID, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPersisted(true)
.build();
// Schedule the job using the JobScheduler
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (jobScheduler != null) {
jobScheduler.schedule(jobInfo);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
and this is Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TestChat"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".PusherService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true"/>
</application>
</manifest>
so how can I make my application received messages when application removed from recent app