Advertisement
Guest User

FCM

a guest
Jun 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. package com.ngantre.dot;
  2.  
  3. import android.app.NotificationChannel;
  4. import android.app.NotificationManager;
  5. import android.content.Intent;
  6. import android.content.SharedPreferences;
  7. import android.graphics.Color;
  8. import android.os.Build;
  9. import android.support.v4.content.LocalBroadcastManager;
  10. import android.util.Log;
  11.  
  12. import com.android.volley.AuthFailureError;
  13. import com.android.volley.Request;
  14. import com.android.volley.RequestQueue;
  15. import com.android.volley.Response;
  16. import com.android.volley.VolleyError;
  17. import com.android.volley.toolbox.StringRequest;
  18. import com.google.firebase.messaging.FirebaseMessaging;
  19. import com.google.firebase.messaging.FirebaseMessagingService;
  20. import com.google.firebase.messaging.RemoteMessage;
  21.  
  22. import org.json.JSONArray;
  23. import org.json.JSONException;
  24. import org.json.JSONObject;
  25.  
  26. import java.util.HashMap;
  27. import java.util.Map;
  28.  
  29. import static com.android.volley.toolbox.Volley.newRequestQueue;
  30.  
  31. public class FirebaseMessagingSrvc extends FirebaseMessagingService {
  32.  
  33.     String idPoint, idServis, antreanMine, antreanNow, title, body, pointName, servisName, kodeNomor;
  34.     SharedPreferences sp;
  35.     SharedPreferences.Editor ed;
  36.  
  37.     @Override
  38.     public void onMessageReceived(RemoteMessage remoteMessage) {
  39.         super.onMessageReceived(remoteMessage);
  40.         sp = this.getSharedPreferences("pref", 0);
  41.         ed = sp.edit();
  42.         //title= remoteMessage.getNotification().getTitle();
  43.         //message=remoteMessage.getNotification().getBody();
  44.         System.out.println("ID USER LOGDEDEDED " + sp.getString("iduser", null));
  45.         if (remoteMessage.getData().size() > 0) {
  46.             if (!sp.getString("iduser", "null").equalsIgnoreCase("null")) {
  47.                 kodeNomor = remoteMessage.getData().get("kodeantrean");
  48.                 antreanNow = remoteMessage.getData().get("antrean");
  49.                 idPoint = remoteMessage.getData().get("idPoint");
  50.                 idServis = remoteMessage.getData().get("idServis");
  51.                 System.out.println("GOING TO DEBUG HERE point " + idPoint + " Serv " + idServis + " Now " + antreanNow);
  52.                 //Toast.makeText(getApplicationContext(), "point "+idPoint+"\nServ "+idServis+"\nNow "+antreanNow, Toast.LENGTH_SHORT).show();
  53.                 getMyNumber();
  54.                 broadcastIntent();
  55.             }
  56.         } else
  57.             System.out.println("GAK DAPET NOTIF");
  58.     }
  59.  
  60.     private void broadcastIntent() {
  61.         Intent intent = new Intent();
  62.         intent.setAction("refreshAdapter");
  63.         Log.d("BROADCASTING meessage", "LOLOLOLOLOLOLOLOLOLOLOLO");
  64.         LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  65.     }
  66.  
  67.     private void getMyNumber() {
  68.         RequestQueue rq = newRequestQueue(getApplicationContext());
  69.         StringRequest sq = new StringRequest(Request.Method.POST, Globals.APIURL, new Response.Listener<String>() {
  70.             @Override
  71.             public void onResponse(String response) {
  72.                 System.out.println("RES NO GUA " + response);
  73.                 try {
  74.                     JSONArray ja = new JSONArray(response);
  75.                     for (int i = 0; i < ja.length(); i++) {
  76.                         JSONObject jo = ja.getJSONObject(i);
  77.                         antreanMine = jo.getString("nomer");
  78.                         servisName = jo.getString("namaservis");
  79.                         pointName = jo.getString("namapoint");
  80.                     }
  81.                 } catch (JSONException e) {
  82.                     e.printStackTrace();
  83.                 }
  84.                 showNotification();
  85.             }
  86.         }, new Response.ErrorListener() {
  87.             @Override
  88.             public void onErrorResponse(VolleyError error) {
  89.                 error.printStackTrace();
  90.             }
  91.         }) {
  92.             @Override
  93.             protected Map<String, String> getParams() throws AuthFailureError {
  94.                 Map<String, String> params = new HashMap<>();
  95.                 params.put("action", "notif");
  96.                 params.put("idpoint", idPoint);
  97.                 params.put("idservis", idServis);
  98.                 params.put("idorang", Globals.IDUSER);
  99.                 return params;
  100.             }
  101.         };
  102.         rq.add(sq);
  103.     }
  104.  
  105.     private void showNotification() {
  106.         if (antreanNow != null && !antreanNow.isEmpty() && antreanMine != null && !antreanMine.isEmpty()) {
  107.             if ((Integer.parseInt(antreanMine) - Integer.parseInt(antreanNow)) < 0) {
  108.                 title = "Antrean anda sudah lewat";
  109.                 body = "No antrean anda sudah terlewati";
  110.                 String FBTopic = pointName.replaceAll("\\s", "") + "On" + servisName.replaceAll("\\s+", "");
  111.                 FBTopic.replaceAll("\\\\s+", "");
  112.                 FirebaseMessaging.getInstance().unsubscribeFromTopic(FBTopic);
  113.                 notifyUser();
  114.             } else if ((Integer.parseInt(antreanMine) - Integer.parseInt(antreanNow)) == 0) {
  115.                 title = "Sekarang Giliran Anda!";
  116.                 body = "Silahkan maju ke counter";
  117.                 notifyUser();
  118.             } else if ((Integer.parseInt(antreanMine) - Integer.parseInt(antreanNow)) <= 5) {
  119.                 title = "No antrean anda sudah dekat!";
  120.                 body = "sekarang sudah no : " + kodeNomor + "" + antreanNow;
  121.                 notifyUser();
  122.             } else {
  123.                 title = "";
  124.                 body = "";
  125.             }
  126.         }
  127.     }
  128.  
  129.     private void notifyUser() {
  130.         System.out.println("nomer gua " + antreanMine + " NO SEkarang " + antreanNow);
  131.         System.out.println("HASIL KALI KALI " + (Integer.parseInt(antreanMine) - Integer.parseInt(antreanNow)));
  132.         if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O) {
  133.             NotificationManager nMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  134.             int importance = NotificationManager.IMPORTANCE_HIGH;
  135.             NotificationChannel nChan = new NotificationChannel(Globals.NOTIFICATION_CHANNEL_ID, Globals.NOTIFICATION_CHANNEL_NAME, importance);
  136.             nChan.setDescription(Globals.NOTIFICATION_CHANNEL_DESCRIPTION);
  137.             nChan.enableLights(true);
  138.             nChan.setLightColor(Color.YELLOW);
  139.             nChan.enableVibration(true);
  140.             nChan.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
  141.             nMan.createNotificationChannel(nChan);
  142.         }
  143.         NotificationMan.getInstance(getApplicationContext()).showNotification(title, body);
  144.     }
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. //=====================================================INSTANSCE ID====================================================================
  154.  
  155. package com.ngantre.dot;
  156.  
  157. import android.util.Log;
  158.  
  159. import com.google.firebase.iid.FirebaseInstanceId;
  160. import com.google.firebase.iid.FirebaseInstanceIdService;
  161.  
  162. public class FireBaseInstance extends FirebaseInstanceIdService {
  163.     @Override
  164.     public void onTokenRefresh() {
  165.         super.onTokenRefresh();
  166.         String token= FirebaseInstanceId.getInstance().getToken();
  167.         //Better using shared preference here to save token, then when user login update the token!
  168.         Log.d("New Token : ",token);
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement