public class MyFirebaseMessagingService extends FirebaseMessagingService {
public String notificacion;
public static final String TAG = "NOTICIAS";
//String nota_push;
String type;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
type = "json";
sendNotificatio(remoteMessage.getData().toString());
}
if (remoteMessage.getNotification() != null) {
type = "message";
mostrarNotificacion(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
sendNotificatio(remoteMessage.getNotification().getBody());
}
}
private void mostrarNotificacion(String title, String body) {
//Intent intent = new Intent(this, MainActivity.class);
//Aqui intento seleccionar la actividad que se abre con el push
Intent intent = new Intent(this, PushActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
private void sendNotificatio(String messageBody) {
String id = "";
String message = "";
String titles = "";
if (type.equals("json")) {
try {
JSONObject jsonObject = new JSONObject(messageBody);/*
id = jsonObjectLog.getStringd("id");
messageTAG, ="fari"+ jsonObject.getString("message"body);
titlesnotificacion = jsonObject.getString("title");
//aquí envío el String a la la otra actividadbody;
Intent i = new Intent(this, PushActivity.class);
i.putExtra("ms", messagenotificacion);
startActivity(i);
} catch (JSONException e) {
e.printStackTrace();
}
} else if (type.equals("message")) {
message = messageBody;
}
*/
}
}
public class PushActivity extends AppCompatActivity {
private static final String TAG = "ivan";
public TextView tx_push;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_push);
String msm = getIntent().getExtras().getString("ms");
tx_push = (TextView) findViewById(R.id.push_id);
tx_push.setText(msm);
Log.d(TAG, "ivan"+ msm);
/*
Bundle/String parametrosmsm = this.getIntent().getExtras();
if(parametros !=null){
String datos = parametros.getString("ms");
//tx_push.setText(datosmsm);
//Log.d(TAG, "ivan"+ datosmsm);
}
*/
}
}
}