Skip to content

Instantly share code, notes, and snippets.

@pratikbutani
Last active March 24, 2022 11:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pratikbutani/5bb53db6b421af25541721229b9a8e74 to your computer and use it in GitHub Desktop.
Save pratikbutani/5bb53db6b421af25541721229b9a8e74 to your computer and use it in GitHub Desktop.
Android - Parse JSON Array from FCM Notifications : https://stackoverflow.com/a/55428420/1318946
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMessagingServ";
private static int NOTIFICATION_ID = 1;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "remoteMessage.getData() : " + remoteMessage.getData());
JSONObject object = new JSONObject(remoteMessage.getData());
Log.d(TAG, "new JSONObject(remoteMessage.getData()) : " + object.toString());
String finalJSON = object.toString().replaceAll("\\\\", "").replace("\"[", "[").replace("]\"", "]");
Log.d(TAG, "Replace all back slash and invalid double quotes : " + finalJSON);
CommonNotificationBean bean = new Gson().fromJson(finalJSON, CommonNotificationBean.class);
sendNotification(bean);
}
}
@Override
public void onNewToken(String s) {
super.onNewToken(s);
Log.d("TAG", "TOKEN : " + s);
Preference.setPushToken(this, s);
}
private void sendNotification(CommonNotificationBean notificationBean) {
String textTitle = notificationBean.getTitle();
String notificationType = notificationBean.getNotificationType();
Intent intent = new Intent(this, DriverStatusActivity.class);
switch (notificationType) {
case NOTIFICATION_ACCEPT_REQUEST:
intent.putExtra(NOTIFICATION_ACCEPT_REQUEST, new Gson().toJson(notificationBean));
break;
case Constants.NOTIFICATION_ARRIVE_REQUEST:
break;
case Constants.NOTIFICATION_START_REQUEST:
break;
case Constants.NOTIFICATION_END_REQUEST:
break;
case Constants.NOTIFICATION_ACCEPT_SCHEDULE_REQUEST:
break;
case Constants.NOTIFICATION_LATER_START_NOTIFICATION_USER:
break;
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, intent, PendingIntent.FLAG_ONE_SHOT);
String channelName = getString(R.string.app_name);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelName)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(textTitle)
.setContentText(notificationBean.getAlert())
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.app_name);
String description = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channelName, name, importance);
channel.setDescription(description);
AudioAttributes attributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
channel.enableLights(true);
channel.enableVibration(true);
channel.setSound(soundUri, attributes);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(NOTIFICATION_ID++, mBuilder.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment