首页 > 解决方案 > 如何查看活动中的推体?

问题描述

我有这个问题:

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

}


}

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

标签: androidfirebaseandroid-intentpush-notificationfirebase-cloud-messaging

解决方案


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

在您的班级中设置一个包含广播管理器的IntentFirebaseMessagingService,您可以使用 a 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);
}

推荐阅读