0

Estoy utilizando FCM para recibir notificaciones (push) en mi App, (aunque funciona bien), lo que quiero es capturar la notificación que llega y quiero colocar el mensaje en un TextView el cual se encuentra en el MainActivity.

Aquí el código:

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getData().size() > 0) {
            try {
                JSONObject json = new JSONObject(remoteMessage.getData().toString());
                sendPushNotification(json);
            } catch (Exception e) {
                Log.e(TAG, "Exception: " + e.getMessage());
            }
        }
    }
    private void sendPushNotification(JSONObject json) {
        try {
            JSONObject data = json.getJSONObject("data");

            String title = data.getString("title");
            String message = data.getString("message");
            String imageUrl = data.getString("image");

            MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    /*Cada vez que llega una notificacion, quiero añadir 
AQUIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
el "message" en un listview el cual está en un fragment*/


                mNotificationManager.showBigNotification(title, message, imageUrl, intent);

        } catch (JSONException e) {
            Log.e(TAG, "Json Exception: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }

}
2
  • En un TextView pero este donde se encuentra? un AlertDialog, Custom Notification, un layout en MainActivity debes agregar más datos a tu pregunta.
    – Jorgesys
    el 8 may. 2017 a las 19:16
  • @Jorgesys ya corregí la pregunat, espero ahora si lo entiendas, espero te hayas molestado en leer la respuesta porque está clarísimo.
    – Jorny
    el 9 may. 2017 a las 0:54

1 respuesta 1

0

Por fin encontré la solución:

AndroidManifest.xml: Añadir android:launchMode="singleTask", esto es muy importante sino no FUNCIONARÁ.

<activity android:name="com.example.jorny.push.MainActivity" android:launchMode="singleTask">
    <intent-filter>
         <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
 </activity>

Código que va en la parte de los comentarios (ver pregunta)

intent.putExtra("ElMensaje", message);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);

MainActivity.java

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        processExtraData();
    }
protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        processExtraData();
    }
private void processExtraData(){
        Bundle b = getIntent().getExtras();
        if (b != null) {
            String sMensaje = b.getString("ElMensaje");
//mostrar mensaje en TextView
            txtMsg.setText(someData);
        }
    }

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.