My app currently schedules notifications for tasks at specific times (chosen by the user). The task's title and reminder date is saved in the Firebase Realtime database.
The point is, when the device reboots, all of that is lost. So it is required to restart all the relevant notifications that were scheduled in the past.
Therefore, I have a BroadcastReceiver that can get the BOOT_COMPLETED intent. However, I can't access Firebase DB, because no user is currently logged in - and I also can't start Firebase Authentication (startActivityForResult isn't recognized and importing it causes more errors). I assume that's because the BroadcastReceiver isn't an activity.
I'm wondering if there is a way around that. I currently tried setting an intent to start the MainActivity (where the user is authenticated) and then perform my relevant tasks, however that did not work.
I'm wondering if there is a way around that, to get the authenticated user (or authenticate him on device reboot) and then get the data from Firebase.
AlarmReceiver code (extends BroadcastReceiver) - it's a bit of a mess but it helps get context.
public class AlarmReceiver extends BroadcastReceiver {
private ChildEventListener mChildEventListener;
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mTaskDatabaseReference;
private FirebaseAuth mFirebaseAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
private int i;
@Override
public void onReceive(final Context context, Intent intent) {
mFirebaseDatabase = FirebaseDatabase.getInstance();
mFirebaseAuth = FirebaseAuth.getInstance();
if (intent.getAction() != null && context != null) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
TaskInfoFragment.showReminderNotification(context, MainActivity.class, "hoooo",1000);
onSignedOutCleanup(context);
mTaskDatabaseReference=mFirebaseDatabase.getReference().child("users").child(MainActivity.getCurrentUserId());
attachDatabaseReadListener(context);
//TODO - HANDLE DEVICE REBOOT
}
}
//Trigger the notification
Bundle extras = intent.getExtras();
String taskTitle = "Error, no task title!";
int taskIntId=-1;
if (extras != null) {
taskTitle = extras.getString("taskTitle");
taskIntId=extras.getInt("taskIntId");
}
TaskInfoFragment.showReminderNotification(context, MainActivity.class, taskTitle,taskIntId);
}
private void attachDatabaseReadListener(final Context context) {
i=0;
mChildEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
i++;
TaskList task = dataSnapshot.getValue(TaskList.class);
TaskInfoFragment.showReminderNotification(context, MainActivity.class, task.getTitle(),i);
Log.d("here is another task","title: "+task.getTitle());
}
public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
public void onCancelled(DatabaseError databaseError) {}
};
mTaskDatabaseReference.addChildEventListener(mChildEventListener);
}
private void onSignedInInitialize(final String userId,Context context) {
//Get reference for the task list for the logged in user and attach the database listener
mTaskDatabaseReference=mFirebaseDatabase.getReference().child("users").child(userId);
attachDatabaseReadListener(context);
}
private void onSignedOutCleanup(Context context) {
Intent taskIntent = new Intent(context,MainActivity.class);
// Send the intent to launch a new activity
context.startActivity(taskIntent);
}
private void detachDatabaseReadListener() {
if (mChildEventListener != null) {
mTaskDatabaseReference.removeEventListener(mChildEventListener);
mChildEventListener = null;
}
}