• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Run AWSiotMqtt subscription in android service

amar94

Lurker
Sep 21, 2019
1
0
Hi all,
I want run aws iot mqtt subscription in android service. When i was run the application in device,app didn't get message which is sent from client.
My service file:

public class NotificationService extends Service {

private AWSIotMqttManager mqttManager;
Context context;
SharedPreferences sharedPreferences;
String clientId;
public NotificationService(){

}

@override
public void onCreate() {
super.onCreate();
sharedPreferences = getSharedPreferences("ClientId",Context.MODE_PRIVATE);
clientId = sharedPreferences.getString("id","");
Log.d(LOG_TAG,"ClientId =="+clientId);
}

@nullable
@override
public IBinder onBind(Intent intent) {
return null;
}

@override
public int onStartCommand(Intent intent, int flags, int startId) {
startMqttSubscription();
return START_STICKY;
}

private void startMqttSubscription() {
mqttManager = new AWSIotMqttManager(clientId,"end-point");
Log.d(LOG_TAG,"**message service is created**");
try {
mqttManager.subscribeToTopic("home/frontdoor/lock", AWSIotMqttQos.QOS0, new AWSIotMqttNewMessageCallback() {
@override
public void onMessageArrived(String topic, byte[] data) {
try {
String msg = new String(data,"UTF-8");
Log.d(LOG_TAG,"message"+msg);
Log.d(LOG_TAG,"topic"+topic);
Toast.makeText(getApplicationContext(),"messages arrived="+msg,Toast.LENGTH_SHORT).show();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
Log.e(LOG_TAG, "Subscription error.", e);
}

}

@override
public void onDestroy() {
super.onDestroy();
}
}
Please can anybody help me.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones