Skip to content Skip to sidebar Skip to footer

Show A Dialog When Firebase Notification Is Received And App Is In The Background

I have an app that elderly people use, and I need a really noticeble way for them to know that they received a new notification, so I want to display a dialog in the screen even th

Solution 1:

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

privatevoidsendNotification(String messageTitle,String messageBody) {
    Intentintent=newIntent(this, DeliveryRecieved.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntentpendingIntent= 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.BuildernotificationBuilder=newNotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_dart_board)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setVibrate(pattern)
            .setLights(Color.BLUE,1,1)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManagernotificationManager= (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;

@OverrideprotectedvoidonCreate(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();
        newMaterialDialog.Builder(this)
                .items(title)
                .itemsCallbackMultiChoice(null, newMaterialDialog.ListCallbackMultiChoice() {
                    @OverridepublicbooleanonSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {

                        mediaPlayer.stop();
                        c.finish();
                        returntrue;
                    }
                })
                .title("NOTICES For Delivery")
                .content("IMPORTANT NOTICE")
                .positiveText("Accept")
                .build().show();


    }

Solution 2:

Create your class which extends FirebaseMessagingService and write onMessageReceived method from which you can show notification dialog or what ever you like to do like :

publicclassMyFirebaseMessagingServiceextendsFirebaseMessagingService {

 @OverridepublicvoidonMessageReceived(RemoteMessage remoteMessage) {
      }
}

Solution 3:

  1. create MyFirebaseMessagingService extends FirebaseMessagingService with its entry in the AndroidManifest and with the method onMessageReceived.

  2. send a data-message (not a notification / display message).

  3. use context.startActivity() to launch an activity

  4. customize the style of the activity to look like a dialog

see: Android Activity as a dialog

Solution 4:

If you want to show dialog you have to either show it in activity by attaching window or by Redirecting to an Activity that will look like a Dialog. Or simply you can use Toast on a handler.

Post a Comment for "Show A Dialog When Firebase Notification Is Received And App Is In The Background"