I'm using Firebase to send messages to my Android app.
After this messages has been received (which works) - I want to open an AlertDialog
.
From what I understood so far, receiving the message happens in a worker thread, so I need to hook into the main thread again. I build Handler
with a Runnable
for this.
How can I retrieve my MainActivity
context inside the runnable?
I looked up solutions like that Context inside a Runnable
But the MainActivity
is not the outer class when Firebase sends the messages.
Here is my setup
MainActivity
package com.example.xhallix.passamessage;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.example.xhallix.passamessage.notifications.FireBaseMessagingServiceManager;
import com.example.xhallix.passamessage.notifications.MyApplication;
import com.example.xhallix.passamessage.notifications.NotificationManager;
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = "CustomLog";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FireBaseMessagingServiceManager fbMessagingService = new FireBaseMessagingServiceManager(this);
}
}
FireBaseMessagingServiceManager
package com.example.xhallix.passamessage.notifications;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import com.example.xhallix.passamessage.MainActivity;
import com.example.xhallix.passamessage.R;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import android.os.Handler;
public class FireBaseMessagingServiceManager extends FirebaseMessagingService {
private static final String LOG_TAG = "CustomLog";
private Context context;
public FireBaseMessagingServiceManager(){
retrieveCurrentToken();
// this is called when firebase is sending data
}
public FireBaseMessagingServiceManager(Context context){
this.context = context; // this was just a test - this is not called when firebase sends the message again , so this.context will be null in onMessageReceive
}
/**
* Get the Token of GoogleFirebase for that device
*/
private void retrieveCurrentToken() {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
Log.w(LOG_TAG, "getInstanceId failed", task.getException());
return;
}
// Get new Instance ID token
String token = task.getResult().getToken();
Log.d(LOG_TAG, token);
}
});
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(LOG_TAG, remoteMessage.getFrom());
Log.d(LOG_TAG,"Notification Message Body: " + remoteMessage.getNotification().getBody());
createAlert();
}
@Override
public void onNewToken(String token) {
Log.d(LOG_TAG, "Refreshed token: " + token);
}
protected void createAlert() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(**???**); // HOW TO RETRIEvE THE CONTEXT FROM MainActivity
builder.setMessage(R.string.alert_dialog_msg);
builder.setTitle(R.string.alert_dialog_title);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.d(LOG_TAG, "Accepted Dialog");
}
});
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.d(LOG_TAG, "NOT Accepted Dialog");
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
}
}