0

I want make my firebase notifications clickable. After clicking the notification, a new activity called HomeActivity will open where i want to show the notification message as a textview.

I've added the notification & it is working well. But whenever i click the notification it goes back to the mainActivity & doesn't show the message in textview. I've tried some code but its not working.

here is my code,

MyFirebaseInstanceService.class

public class MyFirebaseInstanceService extends FirebaseMessagingService {


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    shownotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
    String click_action = remoteMessage.getNotification().getClickAction();
    Intent i = new Intent(click_action);
    Intent in = new Intent("intentKey");
            in.putExtra("key", remoteMessage);
    LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(in);

    Intent intent = new Intent("com.push.message.received");
    intent.putExtra("message", remoteMessage);// Add more data as per need
    sendBroadcast(intent);

}

private void shownotification(String title,String body){
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "com.example.theroos.simplifiedmsging02.test";

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification",NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.setDescription("SIMPLIFIED");
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0,100,500,1000});
            notificationChannel.enableLights(true);
            //notificationChannel.createNotificationChannel(notificationChannel);

        }

        Intent activityintent = new Intent(this,HomeActivity.class);
        PendingIntent contentintent = PendingIntent.getActivity(this,0,activityintent,0);



        NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);

        notificationbuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_mode_comment_black_24dp)
                .setContentTitle(title)
                .setContentIntent(contentintent)
                .setContentText(body)
                .setColor(Color.BLUE)
                .setAutoCancel(true)
                .setContentInfo("Info");

        notificationManager.notify(new Random().nextInt(),notificationbuilder.build());




}

@Override
public void onNewToken(String token) {
    super.onNewToken(token);
    Log.d("TOKEN",token);
}

}

HomeActivity.class

public class HomeActivity extends AppCompatActivity {

private Button signout_button;
private TextView message_textView;
FirebaseAuth mAuth;

private BroadcastReceiver activityReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        TextView message_textview = (TextView) findViewById(R.id.message_textView);
        String message=intent.getStringExtra("message");
        message_textView.setText(message);
    }

};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    signout_button = (Button) findViewById(R.id.signoutbutton);
    //message_textView = (TextView)findViewById(R.id.message_textView);

    //message_textView.setText(getIntent().getExtras().getString("body"));

    if (activityReceiver != null) {
        IntentFilter intentFilter = new  IntentFilter("ACTION_STRING_ACTIVITY");
        registerReceiver(activityReceiver, intentFilter);
    }

    LocalBroadcastManager.getInstance(HomeActivity.this).registerReceiver(
            activityReceiver, new IntentFilter("intentKey"));

    signout_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FirebaseAuth.getInstance().signOut();
            startActivity(new Intent(HomeActivity.this, MainActivity.class));
            Toast.makeText(HomeActivity.this, "Successfully Signed Out", Toast.LENGTH_SHORT).show();
        }
    });
}

   }

I don't know what wrong i have did please help me. Thank you in advance.

Roo S
  • 11
  • 1
  • 4

2 Answers2

0

There are some ways how you can achieve this. But there are lots of similar questions on stackoverflow.

The method depends on your fcm notification payload. Please see this answer. You'll find all the things you're looking for.

https://stackoverflow.com/a/40185654/7140884

0

At first you shouldnt create notification and sendbroadcast message at the same time. Try this:

        public class MyFirebaseInstanceService extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
               NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "com.example.theroos.simplifiedmsging02.test";

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification",NotificationManager.IMPORTANCE_DEFAULT);
            notificationChannel.setDescription("SIMPLIFIED");
            notificationChannel.setLightColor(Color.BLUE);
            notificationChannel.setVibrationPattern(new long[]{0,100,500,1000});
            notificationChannel.enableLights(true);
            //notificationChannel.createNotificationChannel(notificationChannel);

        }

        Intent activityintent = new Intent(this,HomeActivity.class);
        PendingIntent contentintent = PendingIntent.getActivity(this,0,activityintent,0);

        NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);

        notificationbuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_mode_comment_black_24dp)
                .setContentTitle(title)
                .setContentIntent(contentintent)
                .setContentText(body)
                .setColor(Color.BLUE)
                .setAutoCancel(true)
                .setContentInfo("Info");

        notificationManager.notify(new Random().nextInt(),notificationbuilder.build());

    }
....

Then instead of PendingIntent.getActivity you can create PendingIntent.getBroadcast(YourBroadcastReceiver..) for broadcastreceiver which you should register in manifest:

<receiver
    android:name="com.app.YourBroadcastReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="YourReceiverIntentFilter" />
    </intent-filter>
</receiver>

In YourBroadcastReceiver you should get pending intent and then send broadcast message to your local receiver in your activity.

Hope it helps!

ivanovd422
  • 357
  • 5
  • 18