Skip to content

Instantly share code, notes, and snippets.

@Avjeet
Created March 20, 2023 17:55
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 Avjeet/eb3281d3a96cdb889e085cefb4ac64ff to your computer and use it in GitHub Desktop.
Save Avjeet/eb3281d3a96cdb889e085cefb4ac64ff to your computer and use it in GitHub Desktop.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = MyFirebaseMessagingService.class.getSimpleName();
public MyFirebaseMessagingService() {
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().containsKey("default")) {
// Old Rooter notification handling
Timber.i(remoteMessage.getData().get("default") + "");
createNotification(remoteMessage.getData().get("default") + "", this);
} else {
processMessageReceivedFromRendermax(remoteMessage);
NotificationInfo info = CleverTapAPI.getNotificationInfo(remoteMessage.toIntent().getExtras());
if (info.fromCleverTap) {
Intent intent = remoteMessage.toIntent();
Timber.i(intent.getExtras().toString());
if (intent.hasExtra("clevertap_data")) {
try {
JSONObject jsonObject = new JSONObject(intent.getStringExtra("clevertap_data"));
Timber.d("onMessageReceived: " + "%s", jsonObject.toString());
if (jsonObject.has("rooterData")) {
final Notification notification = Notification.getNotification(this, jsonObject, true);
if (notification.isInAppNotification() && AppController.getInstance().getActivityContext() != null) {
intent = new Intent(StaticVars.INTENT_FILTER_BROADCAST_IN_APP_NOTIFICATION);
intent.putExtra(IntentKeys.DATA, notification.getRooterData());
intent.putExtra(IntentKeys.TYPE, notification.getType());
intent.putExtra(IntentKeys.META_DATA, notification.getIntent());
sendBroadcast(intent);
} else {
CleverTapAPI.createNotification(this, intent.getExtras());
}
} else {
CleverTapAPI.createNotification(this, intent.getExtras());
}
} catch (JSONException e) {
CleverTapAPI.createNotification(this, intent.getExtras());
e.printStackTrace();
}
} else {
CleverTapAPI.createNotification(this, intent.getExtras());
}
}
}
}
private void processMessageReceivedFromRendermax(RemoteMessage message) {
if (message.getData().size() > 0) {
Bundle extras = new Bundle();
Iterator var = message.getData().entrySet().iterator();
while (var.hasNext()) {
Map.Entry entry = (Map.Entry) var.next();
extras.putString((String) entry.getKey(), (String) entry.getValue());
}
CleverTapAPI.processPushNotification(getApplicationContext(), extras);
}
}
void createNotification(String json, final MyFirebaseMessagingService context) {
if (SessionManager.getInstance(context).getBooleanPref(StorageVars._LOGGED, false)) {
try {
final JSONObject jsonObject = new JSONObject(json);
final Notification notification = Notification.getNotification(context, jsonObject, true);
if (notification.isInAppNotification() && AppController.getInstance().getActivityContext() != null) {
Intent intent = new Intent(StaticVars.INTENT_FILTER_BROADCAST_IN_APP_NOTIFICATION);
intent.putExtra(IntentKeys.DATA, notification.getRooterData());
intent.putExtra(IntentKeys.TYPE, notification.getType());
intent.putExtra(IntentKeys.META_DATA, notification.getIntent());
sendBroadcast(intent);
} else {
NotificationUtils notificationUtils = new NotificationUtils(context);
notification.setReadStatus(0);
Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a, dd-MM-yyyy");
notification.setTime(simpleDateFormat.format(date));
if (notification.getTag() != null) {
if (notification.getIntent() != null) {
notificationUtils.createNotificationMessage(notification);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onDeletedMessages() {
super.onDeletedMessages();
}
@Override
public void onMessageSent(String s) {
super.onMessageSent(s);
}
@Override
public void onSendError(String s, Exception e) {
super.onSendError(s, e);
}
@Override
public void onNewToken(String s) {
registerGCM(s);
Singular.setFCMDeviceToken(s);
}
/**
* Registering with GCM and obtaining the gcm registration id
*/
private void registerGCM(String token) {
try {
Timber.d("registering gcm");
AppController.getSessionManager().insertStringPref(StorageVars._GCMId, token);
SportsFan user1 = AppController.getSessionManager().getUserDetails();
SportsFan user = new SportsFan();
user.setPushId(token);
CleverTapAPI cleverTap = CleverTapAPI.getDefaultInstance(getApplicationContext());
if (cleverTap != null) {
cleverTap.pushFcmRegistrationId(token, true);
}
if (user1 != null) {
user.setPushId(token);
user1.setPushId(token);
AppController.getSessionManager().setUserDetails(user1);
updateUser(user);
}
Timber.e(token);
} catch (Exception e) {
e.printStackTrace();
}
}
private void updateUser(SportsFan user) {
UserController.getInstance().updateUser(user, new ApiCallback<SportsFan>() {
@Override
public void onResponse(SportsFan sportsFan) {
}
@Override
public void onFail(String reason) {
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment