Reputation: 419
I have implemented firebase cloud messaging in my app. I have been receiving messages sent from server in my device as a push notification but I have not been able to display that message in the textview of the app. How do i do so ?
Here is the code to handle onMessageReceived..
@Override
public void onMessageReceived(RemoteMessage message) {
String image = message.getNotification().getIcon();
title = message.getNotification().getTitle();
text = message.getNotification().getBody();
String sound = message.getNotification().getSound();
int id = 0;
Object obj = message.getData().get("id");
if (obj != null) {
id = Integer.valueOf(obj.toString());
}
this.sendNotification(new NotificationData(image, id, title, text, sound));
}
private void sendNotification(final NotificationData notificationData) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(NotificationData.TEXT, notificationData.getTextMessage());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = null;
notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.toot)
.setContentTitle(URLDecoder.decode(notificationData.getTitle(), "UTF-8"))
.setContentText(URLDecoder.decode(notificationData.getTextMessage(), "UTF-8"))
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (notificationBuilder != null) {
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationData.getId(), notificationBuilder.build());
} else {
Log.d(TAG, "Não foi possível criar objeto notificationBuilder");
}
}
Upvotes: 3
Views: 4719
Reputation: 133
private void handleNotification(String message) {
if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
// app is in foreground, broadcast the push message
Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
pushNotification.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);
// play notification sound
NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.playNotificationSound();
}else{
// If the app is in background, firebase itself handles the notification
}
}
Upvotes: 0
Reputation: 968
You can send a Broadcast message.
In your service:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Bundle bundle = new Bundle();
bundle.putString("msgBody", remoteMessage.getNotification().getBody());
Intent new_intent = new Intent();
new_intent.setAction("ACTION_STRING_ACTIVITY");
new_intent.putExtra("msg", bundle);
sendBroadcast(new_intent);
//other code
}
In your activity:
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver activityReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
TextView textview = (TextView) findViewById(R.id.textview);
Bundle bundle = intent.getBundleExtra("msg");
textview.setText(bundle.getString("msgBody"));
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (activityReceiver != null) {
IntentFilter intentFilter = new IntentFilter("ACTION_STRING_ACTIVITY");
registerReceiver(activityReceiver, intentFilter);
}
}
}
Upvotes: 3