There are lot of questions already available on SO about this topic and I am not saying that I am doing something unique.
In my FirebaseMessagingService
, I am sending Broadcast to show Notification when data message is received.
Here is my code:
public class MessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Message data payload: " + remoteMessage.getData());
String topic = remoteMessage.getData().get(AppConstants.Notification.TOPIC);
if(topic != null) {
switch (topic) {
case FirebaseUtils.TopicMessaging.TOPIC_TASK:
String assignedTo = remoteMessage.getData().get(AppConstants.Notification.ASSIGNED_TO);
//String empId = remoteMessage.getData().get(AppConstants.Notification.EMP_ID);
//String assignedBy = remoteMessage.getData().get(AppConstants.Notification.ASSIGNED_BY);
String title = remoteMessage.getData().get(AppConstants.Notification.TITLE);
String body = remoteMessage.getData().get(AppConstants.Notification.BODY);
if (assignedTo.equals(FotApplication.Employee.Id)) {
//sendNotification(body, title);
Intent intent = new Intent("MY_INTENT_FILTER");
intent.putExtra(AppConstants.Notification.ASSIGNED_TO, assignedTo);
intent.putExtra(AppConstants.Notification.TITLE, title);
intent.putExtra(AppConstants.Notification.BODY, body);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(intent);
}
break;
}
}
}
In AndroidManifest.xml
<receiver
android:name=".NotificationReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="MY_INTENT_FILTER"/>
</intent-filter>
</receiver>
NotificationReceiver
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent != null){
String body = null, title = null;
if(intent.hasExtra(AppConstants.Notification.BODY)){
body = intent.getStringExtra(AppConstants.Notification.BODY);
}
if(intent.hasExtra(AppConstants.Notification.TITLE)){
title = intent.getStringExtra(AppConstants.Notification.TITLE);
}
if(body != null && title != null) {
sendNotification(context, body, title);
}
}
}
private void sendNotification(Context context, String message, String title) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
NotificationChannel channel;
if (Build.VERSION.SDK_INT >= 26) {
channel = new NotificationChannel("default",
"Task",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Task Channel");
notificationManager.createNotificationChannel(channel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, "default")
.setSmallIcon(R.drawable.ic_notification_new)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setVibrate(new long[]{0, 500})
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
notificationManager.notify(0, notificationBuilder.build());
}
}
}
NOTE I have tried everything from sending notification from FCM Json to receive it in FirebaseMessagingService
but when App is closed (closed mean neither in Foreground nor Background) then notifications did not show up. Though data message is delivered to System tray.
The last thing I am trying to do is sending Broadcast but that's not working as well. Might be my bad. I have gone through several links and implemented every solution but that Notification thing haven't shown up anywhere. Don't know how big giants (WhatsApp, Facebook, Microsoft, Google) are doing it.
- How to Send BroadCast from one app to another app
- Why is my broadcast receiver not working
- Broadcast and Broadcast receiver
- FCM Messaging Concept
- Firebase cloud messaging notification not received by device
- enter link description here
Is there any workaround then let me know!