I am working with Firebase to send notifications to a user. I am able to send a basic notification perfectly fine. However, I am having trouble with turning that notification into a heads up notification. I have tried multiple solutions from StackOverflow, but none of them have worked with me.
Below is the respective class I have for receiving push notifications.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(message);
NotificationManager mNotificationManager = getSystemService(NotificationManager.class);
NotificationChannel channel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channel = new NotificationChannel("channel",
"Channel description",
NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), "channel")
.setSmallIcon(R.drawable.googleg_standard_color_18)
.setContentTitle(message.getNotification().getTitle())
.setContentText(message.getNotification().getBody())
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH);
mNotificationManager.notify(0, mBuilder.build());
}
}
You should add your listener service, as you would in a standard GCM implementation.
Run code snippetExpand snippet
Then, register your receiver in AndroidManifest.xml tag to listen on incoming notifications:
This way - you won't have to handle incoming messages separately for cases when app is in foreground vs background.