The aim is to make a notification show up as a notification and the contents of the notification also show on the main activity using firebase. I have tried using intents but it doesn't work. Any help will be much appreciated.
Here is the sender code (MyfirebaseMessagingService)
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
import android.os.Bundle;
import android.widget.TextView ;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
String dodo = "Kwifo";
String message = "Wan Ma bu";
/* String title = remoteMessage.getData().get("message").toString() ;
String message = remoteMessage.getData().get("data").toString() ;*/
// String title1 = remoteMessage.getData().get("title").toString();
Intent intent = new Intent(this, MyFirebaseMessagingService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("data",dodo); //i will love to send data to the MainActivity
intent.putExtra("message",message);
startActivity(intent);
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Body: " + remoteMessage.getNotification().getBody());
String msg = remoteMessage.getNotification().getBody();
Toast.makeText(MyFirebaseMessagingService.this, msg, Toast.LENGTH_SHORT).show();
}
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody())
.setSmallIcon(R.mipmap.ic_launcher)
.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
manager.notify(123, notification);
}
}
and here is the receiver code (MainActivity)
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.TextView ;
import android.app.PendingIntent ;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.gson.Gson;
import retrofit2.Call;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public static final String TOPIC = "/topics/deals";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getIntent().getExtras() != null) {
for (String data : getIntent().getExtras().keySet()) {
String title = getIntent().getExtras().getString("data"); //from MyFirebaseMessagingService
// String content = getIntent().getExtras().getString("message","Default message");
//Log.d(TAG, "Key: " + Message + " Value: " + title);
// String title =getIntent().getStringExtra("data");
String content =getIntent().getStringExtra("message");
TextView tvNotify = findViewById(R.id.tradeidea) ;
tvNotify.setText(title) ;
// tvNotify.setText( "Key: " + title + " Value: " + content) ;
Toast.makeText(MainActivity.this, content,Toast.LENGTH_SHORT).show();
}
}
FirebaseMessaging.getInstance().subscribeToTopic(TOPIC)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = ("Ready to recieve Trading Data");
if (!task.isSuccessful()) {
msg = getString(R.string.msg_subscribe_failed);
}
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});
}
}