0

I want put textview header and text of the notification.

I send push notification with firebase and always comes and see header and text of the notifictaion on notification panel.(This is no problem) My app consist of:MainActivity,Main2Activity and FirebaseMessaginService class. I have button on the MainActivity. When this button clicked opening Main2Activity. I have textview on the Main2Activity . I set textview header and text of the notfication. 1.When App is open. I send notification and notification come. Clicking on the notification will open Main2Activity and I see the text and header of the notification in on the textview. 2.When program is closed and background. My Problem: Again I send notification and notification come. But when clicked notfication does not go to Main2Activity. Goes to MainActivity. I click on the button to check. And goes to Main2Activity and I see textview is null. The following is my FirebaseMessagingService class.

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService{
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String messageTitle = remoteMessage.getNotification().getTitle();
        String messageBody = remoteMessage.getNotification().getBody();

        Log.e("header",messageTitle);
        Log.e("body",messageBody);
        createNotificationChannel(messageTitle,messageBody);
    }

    private void createNotificationChannel(String header,String body) {

        // Create an explicit intent for an Activity in your app
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        intent.putExtra("dataMessage",header);
        intent.putExtra("dataFrom",body);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(header)
                .setContentText(body)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);

        // 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 = "channel_name";
            String description = "channel_description";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(getString(R.string.default_notification_channel_id), name, importance);
            channel.setDescription(description);
            // 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);
            notificationManager.notify(1, builder.build());
        }

    }
}

0 Answers0