Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ngantre.dot;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.graphics.Color;
- import android.os.Build;
- import android.support.v4.content.LocalBroadcastManager;
- import android.util.Log;
- import com.android.volley.AuthFailureError;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import com.google.firebase.messaging.FirebaseMessaging;
- import com.google.firebase.messaging.FirebaseMessagingService;
- import com.google.firebase.messaging.RemoteMessage;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.HashMap;
- import java.util.Map;
- import static com.android.volley.toolbox.Volley.newRequestQueue;
- public class FirebaseMessagingSrvc extends FirebaseMessagingService {
- String idPoint, idServis, antreanMine, antreanNow, title, body, pointName, servisName, kodeNomor;
- SharedPreferences sp;
- SharedPreferences.Editor ed;
- @Override
- public void onMessageReceived(RemoteMessage remoteMessage) {
- super.onMessageReceived(remoteMessage);
- sp = this.getSharedPreferences("pref", 0);
- ed = sp.edit();
- //title= remoteMessage.getNotification().getTitle();
- //message=remoteMessage.getNotification().getBody();
- System.out.println("ID USER LOGDEDEDED " + sp.getString("iduser", null));
- if (remoteMessage.getData().size() > 0) {
- if (!sp.getString("iduser", "null").equalsIgnoreCase("null")) {
- kodeNomor = remoteMessage.getData().get("kodeantrean");
- antreanNow = remoteMessage.getData().get("antrean");
- idPoint = remoteMessage.getData().get("idPoint");
- idServis = remoteMessage.getData().get("idServis");
- System.out.println("GOING TO DEBUG HERE point " + idPoint + " Serv " + idServis + " Now " + antreanNow);
- //Toast.makeText(getApplicationContext(), "point "+idPoint+"\nServ "+idServis+"\nNow "+antreanNow, Toast.LENGTH_SHORT).show();
- getMyNumber();
- broadcastIntent();
- }
- } else
- System.out.println("GAK DAPET NOTIF");
- }
- private void broadcastIntent() {
- Intent intent = new Intent();
- intent.setAction("refreshAdapter");
- Log.d("BROADCASTING meessage", "LOLOLOLOLOLOLOLOLOLOLOLO");
- LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
- }
- private void getMyNumber() {
- RequestQueue rq = newRequestQueue(getApplicationContext());
- StringRequest sq = new StringRequest(Request.Method.POST, Globals.APIURL, new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- System.out.println("RES NO GUA " + response);
- try {
- JSONArray ja = new JSONArray(response);
- for (int i = 0; i < ja.length(); i++) {
- JSONObject jo = ja.getJSONObject(i);
- antreanMine = jo.getString("nomer");
- servisName = jo.getString("namaservis");
- pointName = jo.getString("namapoint");
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- showNotification();
- }
- }, new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- error.printStackTrace();
- }
- }) {
- @Override
- protected Map<String, String> getParams() throws AuthFailureError {
- Map<String, String> params = new HashMap<>();
- params.put("action", "notif");
- params.put("idpoint", idPoint);
- params.put("idservis", idServis);
- params.put("idorang", Globals.IDUSER);
- return params;
- }
- };
- rq.add(sq);
- }
- private void showNotification() {
- if (antreanNow != null && !antreanNow.isEmpty() && antreanMine != null && !antreanMine.isEmpty()) {
- if ((Integer.parseInt(antreanMine) - Integer.parseInt(antreanNow)) < 0) {
- title = "Antrean anda sudah lewat";
- body = "No antrean anda sudah terlewati";
- String FBTopic = pointName.replaceAll("\\s", "") + "On" + servisName.replaceAll("\\s+", "");
- FBTopic.replaceAll("\\\\s+", "");
- FirebaseMessaging.getInstance().unsubscribeFromTopic(FBTopic);
- notifyUser();
- } else if ((Integer.parseInt(antreanMine) - Integer.parseInt(antreanNow)) == 0) {
- title = "Sekarang Giliran Anda!";
- body = "Silahkan maju ke counter";
- notifyUser();
- } else if ((Integer.parseInt(antreanMine) - Integer.parseInt(antreanNow)) <= 5) {
- title = "No antrean anda sudah dekat!";
- body = "sekarang sudah no : " + kodeNomor + "" + antreanNow;
- notifyUser();
- } else {
- title = "";
- body = "";
- }
- }
- }
- private void notifyUser() {
- System.out.println("nomer gua " + antreanMine + " NO SEkarang " + antreanNow);
- System.out.println("HASIL KALI KALI " + (Integer.parseInt(antreanMine) - Integer.parseInt(antreanNow)));
- if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
- NotificationManager nMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- int importance = NotificationManager.IMPORTANCE_HIGH;
- NotificationChannel nChan = new NotificationChannel(Globals.NOTIFICATION_CHANNEL_ID, Globals.NOTIFICATION_CHANNEL_NAME, importance);
- nChan.setDescription(Globals.NOTIFICATION_CHANNEL_DESCRIPTION);
- nChan.enableLights(true);
- nChan.setLightColor(Color.YELLOW);
- nChan.enableVibration(true);
- nChan.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
- nMan.createNotificationChannel(nChan);
- }
- NotificationMan.getInstance(getApplicationContext()).showNotification(title, body);
- }
- }
- //=====================================================INSTANSCE ID====================================================================
- package com.ngantre.dot;
- import android.util.Log;
- import com.google.firebase.iid.FirebaseInstanceId;
- import com.google.firebase.iid.FirebaseInstanceIdService;
- public class FireBaseInstance extends FirebaseInstanceIdService {
- @Override
- public void onTokenRefresh() {
- super.onTokenRefresh();
- String token= FirebaseInstanceId.getInstance().getToken();
- //Better using shared preference here to save token, then when user login update the token!
- Log.d("New Token : ",token);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement