Reputation: 31
I have implemented firebase cloud messaging in my app. I have been receiving messages I want to display that message in the textview of the app.
Here is the code to handle onMessageReceived..
public class FcmMessagingService extends FirebaseMessagingService {
private Map<String, String> data;
private static final String TAG="MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
data=remoteMessage.getData();
String message=data.get("message");
String titledata=data.get("title");
ManualNotification(titledata , message);
}
private void ManualNotification(String title , String messageBody){
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("message", messageBody);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Bitmap bmp = BitmapFactory.decodeResource(this.getResources(),R.drawable.splash_img);
Notification.BigPictureStyle bigpicture = new Notification.BigPictureStyle();
bigpicture.bigPicture(bmp);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notifaction)
.setContentTitle(title)
//.setContentText(messageBody)
.setLargeIcon(bmp)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
.setContentText(messageBody).setLights(Color.YELLOW, 300, 300)
.setVibrate(new long[] { 100, 250 })
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Upvotes: 0
Views: 778
Reputation: 27
Manifest, how will look. Firebaserequired 2 service(instances and messages) another one we are using broadcast in it. so need to add anything for receiving option.
Upvotes: 0
Reputation: 21063
Send a broadcast from FcmMessagingService
whenever you need to notify the Activity
about it .
Intent intent = new Intent("com.push.message.received");
intent.putExtra("message", messageBody);// Add more data as per need
sendBroadcast(intent);
In Activity
register a BroadcastReceiver
to receive the event .
BroadcastReceiver receiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// intent will holding data show the data here
String message=intent.getStringExtra("message");
tvNotificationDetails.settext(message);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
registerReceiver(receiver,new IntentFilter("com.push.message.received"));
}
@Override
protected void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
onDestroy()
is not meant to call each time So you probably unregisterReceiver in onStop()
and register it in onStart()
.
Upvotes: 3