-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mayank Narula
committed
Nov 10, 2017
1 parent
57b175a
commit bdb3a55
Showing
8 changed files
with
154 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
App/SmartHome/app/src/main/java/com/solutions/isecpowify/smarthome/DeviceTokenUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.solutions.isecpowify.smarthome; | ||
|
||
import android.support.annotation.NonNull; | ||
import android.util.Log; | ||
import com.google.firebase.iid.FirebaseInstanceId; | ||
import com.google.firebase.iid.FirebaseInstanceIdService; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.HttpUrl; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* Created by mayank on 10/11/17. | ||
*/ | ||
|
||
public class DeviceTokenUpdater extends FirebaseInstanceIdService { | ||
|
||
private static Map <String,String> queryParams; | ||
private static String currentToken = null; | ||
private static OkHttpClient client = new OkHttpClient().newBuilder() | ||
.connectTimeout(30, TimeUnit.SECONDS) | ||
.readTimeout(60, TimeUnit.SECONDS) | ||
.build(); | ||
|
||
private static final Callback resCB = new Callback() { | ||
@Override | ||
public void onFailure(@NonNull Call call, @NonNull IOException e) { | ||
Log.v(Constants.TAG,e.getMessage()); | ||
call.cancel(); | ||
} | ||
|
||
@Override | ||
public void onResponse(@NonNull Call call, @NonNull Response response) | ||
throws IOException { | ||
|
||
if (response.isSuccessful()){ | ||
Log.v(Constants.TAG, String.valueOf(response.body())); | ||
} | ||
} | ||
}; | ||
|
||
|
||
@Override | ||
public void onTokenRefresh() { | ||
|
||
// Get updated InstanceID token. | ||
currentToken = FirebaseInstanceId.getInstance().getToken(); | ||
} | ||
|
||
private static String getDeviceFCMToken(){ | ||
return currentToken; | ||
} | ||
|
||
static void sendDetailsToServer(final String otrk){ | ||
|
||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
while(getDeviceFCMToken()==null) | ||
{ | ||
try { | ||
Thread.sleep(1000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
queryParams = Collections.emptyMap(); | ||
queryParams.put("fcm_token",getDeviceFCMToken()); | ||
queryParams.put("id_otr",otrk); | ||
get(Constants.DEVICE_REG_SERVER,queryParams,resCB); | ||
} | ||
}).start(); | ||
} | ||
|
||
public static void get(String url, Map<String,String> params, Callback responseCallback) { | ||
|
||
try{ | ||
HttpUrl.Builder httpBuilder = HttpUrl.parse(url).newBuilder(); | ||
|
||
if (params != null) { | ||
for(Map.Entry<String, String> param : params.entrySet()) { | ||
httpBuilder.addQueryParameter(param.getKey(),param.getValue()); | ||
} | ||
} | ||
Request request = new Request.Builder().url(httpBuilder.build()).build(); | ||
client.newCall(request).enqueue(responseCallback); | ||
|
||
} catch (NullPointerException obj){ | ||
Log.v(Constants.TAG,"Object is null : " + obj.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters