I may be a bit late but here is a complete solution, which will force the activity to auto open even if the application is closed or killed or in the background. This solution can even show the "Activity/Fragment" when the device is on sleep or screen lock.
Create an activity which will serve the notification and open automatically whenever notification received.
inside your
onMessageReceived
Do following code
if (remoteMessage.getData().size() > 0) {
sendNotification(remoteMessage.getData().get("title"),remoteMessage.getData().get("body"));
}
here your sendNotification() method is
private void sendNotification(String messageTitle,String messageBody) {
Intent intent = new Intent(this, DeliveryRecieved.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0 /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500,500,500,500,500};
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_dart_board)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setVibrate(pattern)
.setLights(Color.BLUE,1,1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
getApplicationContext().startActivity(intent);
}
Notice the two important things
FLAG_ACTIVITY_NEW_TASK
and
getApplicationContext().startActivity(intent);
In your activity, if you want to un-lock device screen and bring the application back from device sleep mode do the following in your activity onCreate()
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
The last important thing is in firebase messaging system always use "data payload" because to invoke onMessageReceived() when the device is in background "data payload is required"
To show any dialogue you can code it in your activity's onCreateView() or anywhere;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final DeliveryRecieved c = this;
setContentView(R.layout.activity_jp_map);
mediaPlayer = MediaPlayer.create(this, R.raw.call_bell);
mediaPlayer.setLooping(true);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
String[] title = {"A","B"};
mediaPlayer.start();
new MaterialDialog.Builder(this)
.items(title)
.itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
mediaPlayer.stop();
c.finish();
return true;
}
})
.title("NOTICES For Delivery")
.content("IMPORTANT NOTICE")
.positiveText("Accept")
.build().show();
}