0

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 though the App might be closed/Background

I created a class that extends Dialog to show a Dialog, and it works when I call it in any of my activities:

The error makes sense since FirebaseMessagingService is actually not one of my classes, but I do not know how to work around this, inputs are appreciated

Thanks

dfabiano
  • 82
  • 1
  • 7
  • What you are trying to do here is to draw over other apps. For this you'll need the SYSTEM_ALERT_WINDOW , which at API23+ needs to be explicitly enabled in system settings. This is a huge security liability and you should reconsider whether this is really required. More reading: https://stackoverflow.com/a/32652625/1025599 . As an alternative, you can use `NotificationManager` to show a standard notification. – Nachi Jun 21 '17 at 14:31
  • IMHO, having a dialog *pop-up* upon a notification arrival is *bad UX*. It's like a [pop-up ad](https://en.wikipedia.org/wiki/Pop-up_ad). – AL. Jun 22 '17 at 03:00
  • It is, but my app is used for elderly people to help them with their health routine, they do not that they have a new activity unless you make it really noticeable. There is a correct use for this practice. – dfabiano Jun 22 '17 at 13:29

4 Answers4

3

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();


    }
Syed Fahad
  • 137
  • 9
1

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 :

public class MyFirebaseMessagingService extends FirebaseMessagingService {

 @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
      }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ashish Garg
  • 162
  • 1
  • 11
  • That's exatcly what I have, but the line "alert.showNotificationDialog(FirebaseMessagingService.this" gives me an error, and it does the same if i do "myclassname.this". – dfabiano Jun 21 '17 at 14:10
  • Maybe the problem is that my shownotificationDialog function takes an activity to display the activity at... However, what activity do I pass to show the dialog when the activity is in the background? Just taking a guess. I do not know if that is the actual problem. – dfabiano Jun 21 '17 at 14:13
  • If you want to handle push notification message in dialog than you have to use broadcast receiver. – ashish Jun 26 '17 at 13:52
0
  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

Diego Giorgini
  • 12,489
  • 1
  • 47
  • 50
0

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.

Samsad CV
  • 126
  • 10