如何在textview android上显示firebase云消息的推送通知消息

如何在textview android上显示firebase云消息的推送通知消息,android,push-notification,firebase-cloud-messaging,Android,Push Notification,Firebase Cloud Messaging,我已经在我的应用程序中实现了firebase云消息传递。我一直在设备中接收服务器发送的推送通知消息,但我无法在应用程序的文本视图中显示该消息。我该怎么做 下面是处理onMessageReceived的代码 @Override public void onMessageReceived(RemoteMessage message) { String image = message.getNotification().getIcon(); title = message.g

我已经在我的应用程序中实现了firebase云消息传递。我一直在设备中接收服务器发送的推送通知消息,但我无法在应用程序的文本视图中显示该消息。我该怎么做

下面是处理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");
    }
}

您可以发送广播消息

为您服务:

    @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
    }
在您的活动中:

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);
        }
    }
}

我没有看到您试图设置来自通知的
TextView
值的任何代码。关于设置TextView的文本,你试过什么吗?@AL.我试过。。他们中没有一个能把他们写进你的帖子里。缩小出错/遗漏的范围会更容易。如果要在应用程序中向用户显示收到的通知数据,请尝试将其保存在任何本地存储(如sp、sqlite/内容提供商)中,然后在UI中显示它们。
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
        }
    }