· 6 years ago · Sep 16, 2019, 02:18 PM
1package com.pay.proyek;
2
3import android.app.Service;
4import android.content.Context;
5import android.content.Intent;
6import android.content.SharedPreferences;
7import android.net.Uri;
8import android.os.IBinder;
9import android.util.Log;
10import android.widget.Toast;
11
12import androidx.annotation.NonNull;
13
14import com.google.firebase.messaging.FirebaseMessagingService;
15import com.google.firebase.messaging.RemoteMessage;
16import com.google.gson.Gson;
17import com.pay.proyek.network.RetrofitHelper;
18import com.pay.proyek.response.FcmResponse;
19
20import java.net.URI;
21import java.net.URL;
22import java.text.SimpleDateFormat;
23import java.util.Date;
24import java.util.Map;
25
26import retrofit2.Call;
27import retrofit2.Callback;
28import retrofit2.Response;
29
30import static com.pay.proyek.utils.SharedPrefManager.KEY_APIKEY;
31import static com.pay.proyek.utils.SharedPrefManager.KEY_CLIENT;
32import static com.pay.proyek.utils.SharedPrefManager.KEY_ID;
33import static com.pay.proyek.utils.SharedPrefManager.PREF_PAY;
34
35public class FirebaseMessagingServie extends FirebaseMessagingService {
36
37 private static final String TAG = FirebaseMessagingService.class.getSimpleName();
38 enum PUSH_NOTIFICATION_SOURCE{
39 CONSOLE, API_WITHOUT_NOTIFICATION, API_WITH_NOTIFICATION, UNKNOWN_SOURCE;
40 }
41
42 @Override
43 public void onNewToken(@NonNull String s) {
44 super.onNewToken(s);
45 Log.i(getString(R.string.DEBUG_TAG),"New Token : "+s);
46 }
47
48 @Override
49 public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
50 super.onMessageReceived(remoteMessage);
51
52
53 Log.d(getString(R.string.DEBUG_TAG), "onMessageReceived: " + new Gson().toJson(remoteMessage));
54 PUSH_NOTIFICATION_SOURCE notification_source = getNotificationSource(remoteMessage);
55 // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
56 Log.i(getString(R.string.DEBUG_TAG), "From: " + remoteMessage.getFrom());
57 Log.i(getString(R.string.DEBUG_TAG), "Message Recived From : " + notification_source);
58
59 SharedPreferences set = getSharedPreferences(PREF_PAY,MODE_PRIVATE);
60 SharedPreferences.Editor editor = set.edit();
61
62 if (remoteMessage.getNotification().getImageUrl() == null){
63
64 final String api = set.getString(KEY_APIKEY,"");
65 String client = set.getString(KEY_CLIENT,"");
66 String judul = remoteMessage.getNotification().getTitle();
67 String isi = remoteMessage.getNotification().getBody();
68
69 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
70 String joiningDate = sdf.format(new Date());
71
72 // Check if message contains a notification payload.
73 if (remoteMessage.getNotification() != null) {
74 Log.i(getString(R.string.DEBUG_TAG), "Message Notification Title: " + judul);
75 Log.i(getString(R.string.DEBUG_TAG), "Message Notification Body: " + isi);
76 //Log.i(getString(R.string.DEBUG_TAG), "Message Notification ImageUrl: " + img);
77 Log.i(getString(R.string.DEBUG_TAG),"Ini Api key : "+api);
78
79 RetrofitHelper.getService(this)
80 .sentFcm1(joiningDate,judul,isi,client,api)
81 .enqueue(new Callback<FcmResponse>() {
82 @Override
83 public void onResponse(Call<FcmResponse> call, Response<FcmResponse> response) {
84 if (response.isSuccessful()) {
85 Toast.makeText(getApplicationContext(), "Data Terkirim", Toast.LENGTH_SHORT).show();
86 } else {
87 Toast.makeText(getApplicationContext(), "Data Tidak terkirim", Toast.LENGTH_SHORT).show();
88 }
89 }
90 @Override
91 public void onFailure(Call<FcmResponse> call, Throwable t) {
92 Toast.makeText(FirebaseMessagingServie.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
93 }
94 });
95 }
96
97 }else{
98
99 final String api = set.getString(KEY_APIKEY,"");
100 String client = set.getString(KEY_CLIENT,"");
101 String judul = remoteMessage.getNotification().getTitle();
102 String isi = remoteMessage.getNotification().getBody();
103 String img = remoteMessage.getNotification().getImageUrl().toString();
104
105 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
106 String joiningDate = sdf.format(new Date());
107
108 // Check if message contains a notification payload.
109 if (remoteMessage.getNotification() != null) {
110 Log.i(getString(R.string.DEBUG_TAG), "Message Notification Title: " + judul);
111 Log.i(getString(R.string.DEBUG_TAG), "Message Notification Body: " + isi);
112 Log.i(getString(R.string.DEBUG_TAG), "Message Notification ImageUrl: " + img);
113 Log.i(getString(R.string.DEBUG_TAG),"Ini Api key : "+api);
114
115 RetrofitHelper.getService(this)
116 .sentFcm(joiningDate,judul,isi,img,client,api)
117 .enqueue(new Callback<FcmResponse>() {
118 @Override
119 public void onResponse(Call<FcmResponse> call, Response<FcmResponse> response) {
120 if (response.isSuccessful()) {
121 Toast.makeText(getApplicationContext(), "Data Terkirim", Toast.LENGTH_SHORT).show();
122 } else {
123 Toast.makeText(getApplicationContext(), "Data Tidak terkirim", Toast.LENGTH_SHORT).show();
124 }
125 }
126 @Override
127 public void onFailure(Call<FcmResponse> call, Throwable t) {
128 Toast.makeText(FirebaseMessagingServie.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
129 }
130 });
131 }
132
133 }
134
135
136 // Check if message contains a data payload.
137 if (remoteMessage.getData().size() > 0) {
138 Log.i(getString(R.string.DEBUG_TAG), "Message data payload: " + remoteMessage.getData());
139
140 }
141
142 // Also if you intend on generating your own notifications as a result of a received FCM
143 // message, here is where that should be initiated. See sendNotification method below.
144 }
145
146 private PUSH_NOTIFICATION_SOURCE getNotificationSource(RemoteMessage remoteMessage) {
147
148 PUSH_NOTIFICATION_SOURCE notification_source;
149
150 RemoteMessage.Notification notification = remoteMessage.getNotification();
151 Map<String , String> data = remoteMessage.getData();
152
153 if (notification != null && data != null){
154 if (data.size() == 0 ){
155 notification_source = PUSH_NOTIFICATION_SOURCE.CONSOLE;
156 }else{
157 notification_source = PUSH_NOTIFICATION_SOURCE.API_WITH_NOTIFICATION;
158 }
159 }else if(remoteMessage.getData() != null){
160 notification_source = PUSH_NOTIFICATION_SOURCE.API_WITHOUT_NOTIFICATION;
161 }else{
162 notification_source = PUSH_NOTIFICATION_SOURCE.UNKNOWN_SOURCE;
163 }
164 return notification_source;
165 }
166}