Skip to content

Instantly share code, notes, and snippets.

@newarifrh
Created January 10, 2020 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save newarifrh/bf118b6067f770b5dbbc375965dc0ab2 to your computer and use it in GitHub Desktop.
Save newarifrh/bf118b6067f770b5dbbc375965dc0ab2 to your computer and use it in GitHub Desktop.
package ks.stis.simpus.service;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.graphics.Color;
import android.nfc.Tag;
import android.util.Log;
import androidx.core.app.NotificationCompat;
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.sql.SQLOutput;
import java.util.Map;
import ks.stis.simpus.R;
import ks.stis.simpus.apihelper.BaseApiService;
import ks.stis.simpus.apihelper.UtilsApi;
import ks.stis.simpus.model.NotificationUtils;
import ks.stis.simpus.model.Preferences;
import okhttp3.ResponseBody;
import okhttp3.internal.Util;
import retrofit2.Call;
import static ks.stis.simpus.service.App.FCM_CHANNEL_ID;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingService";
private String username;
private NotificationUtils mNotificationUtils;
private BaseApiService apiService;
@Override
public void onCreate() {
super.onCreate();
mNotificationUtils = new NotificationUtils(this);
Preferences preferences = new Preferences(this);
if (preferences.getSPLogedIn()) {
username = preferences.getSpUsername();
} else {
username = "guest";
}
System.out.println(username);
}
@SuppressLint("LongLogTag")
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "onMessageReceived: Called");
Log.d(TAG, "onMessageReceived: Message Received From: " +remoteMessage.getFrom());
if(remoteMessage.getNotification() != null){
String title = remoteMessage.getNotification().getTitle();
String body = remoteMessage.getNotification().getBody();
Notification notification = new NotificationCompat.Builder(this, FCM_CHANNEL_ID)
.setSmallIcon(R.drawable.logo).setContentTitle(title).setContentText(body).setColor(Color.BLUE)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(100,notification);
}
if(remoteMessage.getData().size() > 0){
Log.d(TAG, "onMessageReceived: Data :"+remoteMessage.getData().toString());
}
//
// try {
//
// JSONArray ja = new JSONArray(result);
//
//
// System.out.println(ja.length());
//
// for (int i = 0; i < ja.length(); i++) {
// JSONObject jo = new JSONObject(ja.getString(i));
//
// Notification.Builder nb;
// if (jo.getString("username").equals("ALL")) {
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// nb = mNotificationUtils.getAndroidChannelNotification(jo.getString("title"), jo.getString("message"));
// mNotificationUtils.getManager().notify(101, nb.build());
// System.out.println(0);
// } else {
// Notification.Builder notif = new Notification.Builder(this)
// .setContentTitle(jo.getString("title"))
// .setSmallIcon(R.drawable.logo)
// .setStyle(new Notification.BigTextStyle().bigText(jo.getString("message")));
//
// System.out.println(1);
// mNotificationUtils.getManager().notify(102, notif.build());
// }
//
// break;
// }
//
// if (jo.getString("username").equals(username)) {
//
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// nb = mNotificationUtils.getAndroidChannelNotification(jo.getString("title"), jo.getString("message"));
// mNotificationUtils.getManager().notify(101, nb.build());
// System.out.println(0);
// } else {
// Notification.Builder notif = new Notification.Builder(this)
// .setContentTitle(jo.getString("title"))
// .setSmallIcon(R.drawable.logo)
// .setStyle(new Notification.BigTextStyle().bigText(jo.getString("message")));
//
// System.out.println(1);
// mNotificationUtils.getManager().notify(102, notif.build());
// }
//
//
// break;
// }
// }
//
//
// } catch (JSONException e) {
// e.printStackTrace();
// }
}
@SuppressLint("LongLogTag")
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("NEW_TOKEN", s);
//kirim token disini
apiService = UtilsApi.getAPIService();
Call<ResponseBody> token = apiService.kirimtoken(s);
Log.d(TAG, String.valueOf(token));
}
@SuppressLint("LongLogTag")
@Override
public void onDeletedMessages() {
super.onDeletedMessages();
Log.d(TAG,"onDeletedMessaged:Called");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment