The problem I have is that when clicking on the push notification received in the status bar, the activity that I have selected for this purpose does not open, the main activity continues to open, if you can help me, please check what happens and that it always works. open the PushActivity.class activity.
On the other hand, the objective is to show the notification in a texView, well since the activity is not being sent, however I added a listener just to open that activity and see if the push notification arrives and is placed in the texView but apparently null arrives.
Please I appreciate the help, below the codes.
MyFirebaseMessagingService
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public String notificacion;
public static final String TAG = "NOTICIAS";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
}
if (remoteMessage.getNotification() != null) {
mostrarNotificacion(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
}
private void mostrarNotificacion(String title, String body) {
//Intent intent = new Intent(this, MainActivity.class);
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());
Log.d(TAG, "fari"+ body);
notificacion = body;
Intent i = new Intent(this, PushActivity.class);
i.putExtra("ms", notificacion);
startActivity(i);
}
}
The activity that should be opened and that should receive the parameters of the push notification in the textView is the following:
PushActivity
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);
tx_push = (TextView) findViewById(R.id.push_id);
String msm = getIntent().getExtras().getString("ms");
tx_push.setText(msm);
Log.d(TAG, "ivan"+ msm);
}
}
You only need the method
mostrarNotificacion()
, what this method does is create a notification, what this notification does is that when you click, the Activity opensPushActivity
:if you want to add a message you must define it in this Intent:
You don't need to create another
Intent
, just one which will be defined inPendingIntent
so that clicking on the notification opens the oneActivity
defined inIntent
.I suggest you also validate if no data is received in the
bundle
enPushActivity
: