Using mqtt to send push notification , not working when swiped from recent apps

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Using mqtt to send push notification , not working when swiped from recent apps



using mqtt for push notification in android with following libs :


implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'



i followed this tutorial , the problem is it works properly when app is open , but when it is swiped from recent apps , it stops working and I can't receive push notifications anymore
any help is appreciated



solutions i tried :



first solution : created a service and simply created a mqttclient in it , but after swiping from recent apps didn't work anymore


public class MqttService extends Service

public MqttService(Context context)
super();


public MqttService()


@Override
public int onStartCommand(Intent intent, int flags, int startId)
super.onStartCommand(intent, flags, startId);

PahoDemo paho = new Paho(getApplicationContext(), "myTopic");

return START_STICKY;



@Override
public void onTaskRemoved(Intent rootIntent)

Intent intent = new Intent("com.example.restart");
sendBroadcast(intent);


@Override
public void onDestroy()
super.onDestroy();

Intent intent = new Intent("com.example.restart");
sendBroadcast(intent);


@Nullable
@Override
public IBinder onBind(Intent intent)
return null;





PahoDemo :


public class Paho implements MqttCallback
String topic;
MqttClient client;
Context context;

public Paho(Context context, String topic)
this.context = context;
this.topic = topic;
try
client = new MqttClient(StaticValue.getBROKER(), MqttClient.generateClientId(), persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
client.connect(connOpts);
client.setCallback(this);
if (topic != null)
client.subscribe(topic);

catch (MqttException e)
System.out.println("error: " + e);
e.printStackTrace();


@Override
public void connectionLost(Throwable cause)

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception
Log.i("mqtt", "messageArrived ");


@Override
public void deliveryComplete(IMqttDeliveryToken token)
Log.i("mqtt", token + "");





second solution:



added the below line to manifest :


<service android:name="org.eclipse.paho.android.service.MqttService" />



called this helper class from mainactivty's Oncreate :


public class MqttHelper
public MqttAndroidClient mqttAndroidClient;

final String serverUri = "myBrokerUri";

final String clientId = MqttClient.generateClientId();
final String subscriptionTopic = "myTopic";


public MqttHelper(Context context)
mqttAndroidClient = new MqttAndroidClient(context, serverUri, clientId);
mqttAndroidClient.setCallback(new MqttCallbackExtended()
@Override
public void connectComplete(boolean b, String s)
Log.w("mqtt", s);


@Override
public void connectionLost(Throwable throwable)



@Override
public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception
Log.w("Mqtt", mqttMessage.toString());


@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)


);
connect();


public void setCallback(MqttCallbackExtended callback)
mqttAndroidClient.setCallback(callback);


private void connect()
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setCleanSession(true);

try

mqttAndroidClient.connect(mqttConnectOptions, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)

subscribeToTopic();


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("Mqtt", "Failed to connect to: " + serverUri + exception.toString());

);


catch (MqttException ex)
ex.printStackTrace();




private void subscribeToTopic()
try
mqttAndroidClient.subscribe(subscriptionTopic, 0, null, new IMqttActionListener()
@Override
public void onSuccess(IMqttToken asyncActionToken)
Log.w("Mqtt","Subscribed!");


@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception)
Log.w("Mqtt", "Subscribed fail!");

);

catch (MqttException ex)
System.err.println("Exceptionst subscribing");
ex.printStackTrace();





that's all i did since tow days ago . if someone has any ideas , please mention .





Where do you implement your client? If you want to subscribe a topic when app is closed you will have to create a service, but to make it alive you will have to keep showing notification about that.
– Ikazuchi
Aug 8 at 11:11





at first i wrote my own service , but not working when app swiped from recent apps , second used org.eclipse.paho.android.service.MqttService but got same answer , plz explain your solution more obvious .
– Mohammad cs
Aug 8 at 12:23





Check this stackoverflow.com/a/35684523, if you will still have problem with your implementation of service, just update your question and i will try to help to solve your problem.
– Ikazuchi
Aug 8 at 12:28





i knew that , if you have a solution please post . thanks in advance .
– Mohammad cs
Aug 8 at 14:38





You need to show us what you've tried and we will help you fix it, we will not just wholesale provide a solution
– hardillb
Aug 8 at 15:27




1 Answer
1



when messageArrived is called , you shouldn't use something you don't access to . for example if you wanna check app is in foreground , you may use context , this way your logic may doesn't work properly , so make sure you do what you can do in messageArrived and if you aren't sure the objects you use are available do it another way (also be careful about connectinLost )






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard