【问题标题】:How to see push body in activity?如何查看活动中的推体?
【发布时间】:2018-11-16 10:51:36
【问题描述】:

我有这个问题:

使用 action_click 通过 PHP 面板发送推送。 Activity 会打开,但上面是白屏。

我如何在那里传输数据:标题、消息、图像(如果有,则默认情况下)

如果您能给我一个如何实现它的工作版本,请提前非常感谢。

对不起,我的英语很糟糕,我是俄罗斯人。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

     private static final String TAG = "MyFirebaseMsgingService";
private static final String TITLE = "title";
private static final String EMPTY = "";
private static final String MESSAGE = "message";
private static final String IMAGE = "image";
private static final String ACTION = "action";
private static final String DATA = "data";
private static final String ACTION_DESTINATION = "action_destination";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        Map<String, String> data = remoteMessage.getData();
        handleData(data);

    } else if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
        handleNotification(remoteMessage.getNotification());
    }// Check if message contains a notification payload.

}
private void handleNotification(RemoteMessage.Notification RemoteMsgNotification) {
    String message = RemoteMsgNotification.getBody();
    String title = RemoteMsgNotification.getTitle();
    NotificationVO notificationVO = new NotificationVO();
    notificationVO.setTitle(title);
    notificationVO.setMessage(message);

    Intent resultIntent = new Intent(getApplicationContext(), PushTest.class);
    NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
    notificationUtils.displayNotification(notificationVO, resultIntent);
    notificationUtils.playNotificationSound();
}

private void handleData(Map<String, String> data) {
    String title = data.get(TITLE);
    String message = data.get(MESSAGE);
    String iconUrl = data.get(IMAGE);
    String action = data.get(ACTION);
    String actionDestination = data.get(ACTION_DESTINATION);
    NotificationVO notificationVO = new NotificationVO();
    notificationVO.setTitle(title);
    notificationVO.setMessage(message);
    notificationVO.setIconUrl(iconUrl);
    notificationVO.setAction(action);
    notificationVO.setActionDestination(actionDestination);

    Intent resultIntent = new Intent(getApplicationContext(), PushTest.class);

    NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
    notificationUtils.displayNotification(notificationVO, resultIntent);
    notificationUtils.playNotificationSound();

}


}

如何在我的活动中显示数据? Activity 像 PushTest 一样通过 action_click 打开,看到只有一个白屏

【问题讨论】:

    标签: android firebase android-intent push-notification firebase-cloud-messaging


    【解决方案1】:
    Intent resultIntent = new Intent(this, PushTest.class);
    resultIntent.putExtra("title", title);
    resultIntent.putExtra("message", message);
    resultIntent.putExtra("iconUrl", iconUrl);
    startActivity(resultIntent);
    

    在我的活动中

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pushtest);
        txtTitle = (TextView) findViewById(R.id.textView3);
        txtMessage = (TextView) findViewById(R.id.textView3);
        Intent resultIntent = getIntent();
    
        String title = resultIntent.getStringExtra("title");
        String message = resultIntent.getStringExtra("message");
        String iconUrl = resultIntent.getStringExtra("iconUrl");
        txtTitle.setText(title);
        txtMessage.setText(message);
    }
    

    【讨论】:

      【解决方案2】:

      我会建议你在 Activity 和服务中使用广播接收器而不是意图:

      在您的FirebaseMessagingService 类中设置一个包含广播管理器的Intent,您可以为此使用LocalBroadcastManager

      例如:

      Intent intent = new Intent("BROADCAST_MESSAGE_RECEIVED");
              LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
      

      Activity 中创建BroadcastReceiver 的实例:

      private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context broadcastContext, Intent intent) {
              // Extract data from your intent in here 
              getData(intent);
          }
      };
      

      然后,在您的Activity 类中,注册和注销您的BroadcastReceiver

      @Override
      protected void onResume() {
          super.onResume();
      
          //Register local broadcast receiver for notifications after receiving message
          LocalBroadcastManager.getInstance(this).registerReceiver(mBroadcastReceiver,
                  new IntentFilter("BROADCAST_MESSAGE_RECEIVED"));
      }
      
      @Override
      protected void onPause() {
          super.onPause();
      
          // Unregister broadcast receiver
          LocalBroadcastManager.getInstance(this)
                  .unregisterReceiver(mBroadcastReceiver);
      }
      

      【讨论】:

      • 谢谢解答,上面的系统我已经发明了,好像可以用))
      猜你喜欢
      • 2017-06-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 2012-07-07
      • 2011-06-10
      • 2016-10-22
      • 1970-01-01
      • 2015-08-15
      相关资源
      最近更新 更多