Reputation: 82
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
Upvotes: 0
Views: 9095
Reputation: 125
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.
Upvotes: 0
Reputation: 147
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();
}
Upvotes: 3
Reputation: 12717
create MyFirebaseMessagingService extends FirebaseMessagingService
with its entry in the AndroidManifest and with the method onMessageReceived
.
send a data-message (not a notification / display message).
use context.startActivity() to launch an activity
customize the style of the activity to look like a dialog
see: Android Activity as a dialog
Upvotes: 0
Reputation: 160
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) {
}
}
Upvotes: 1