如何在textview android上显示来自firebase云消息的推送通知消息 [英] how to display message of push notification from firebase cloud messaging on textview android

查看:22
本文介绍了如何在textview android上显示来自firebase云消息的推送通知消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

这里是处理 onMessageReceived 的代码..

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

推荐答案

可以发送Broadcast消息.

You can send a Broadcast message.

为您服务:

    @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 android上显示来自firebase云消息的推送通知消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆