如何看到活动中的推身?

时间:2018-11-16 10:51:36

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

我有这个问题:

使用 action_click 通过 PHP 面板发送推送。 活动已打开,但屏幕上是白屏。

如何在其中传输数据:标题,消息,图像(如果有,默认情况下则没有)

如果您能给我一个实现它的工作版本,请多谢。

对不起,我的英语不好,我是俄语。

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

}


}

如何在活动中显示数据? 通过action_click(如PushTest)打开活动,然后看到只有一个白色屏幕

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我建议您在活动和服务中使用广播接收器,而不要使用意图:

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

例如:

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

BroadcastReceiver中创建Activity的实例:

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