从通知发送消息和标题,以在活动的textview中显示
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从通知发送消息和标题,以在活动的textview中显示相关的知识,希望对你有一定的参考价值。
我已成功使用我自己的服务器向android应用程序发送Firebase通知,我在textview中显示通知(消息和标题)或在其他活动中显示Listview >>我需要帮助才能做到这一点
FcmMessagingService类:
public class FcmMessagingService extends FirebaseMessagingService {
private Map<String, String> data;
private static final String TAG = "MyFirebaseMsgService";
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@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);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
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.ic_launcher_background);
Notification.BigPictureStyle bigpicture = new Notification.BigPictureStyle();
bigpicture.bigPicture(bmp);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_background)
.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());
}
}
答案
你应该看一下广播接收器来自你的服务onMessageReceived();
Intent in = new Intent("intentKey);
in.putExtra("key", notificationString);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(in);
在您的活动中注册onCreate()中的接收器
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
mMessageReceiver, new IntentFilter("intentKey"));
并且出于onCreate()这段代码。
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String messageNotification = intent.getStringExtra("key");
tvStatus.setText(messageNotification);
}
};
以上是关于从通知发送消息和标题,以在活动的textview中显示的主要内容,如果未能解决你的问题,请参考以下文章
如何从服务器发送文本以在 android 的 TextView 中显示?