android - 如何查看活动中的推体?
问题描述
我有这个问题:
使用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 打开,看到只有一个白屏
解决方案
我会建议您在 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);
}
推荐阅读
- html - 如何使用 HTML 和 CSS 在边框中间实现标题
- sockets - 回调功能是否在内部使用 select() 调用?
- azure - Howto:Azure DevOps YAML 中的动态变量名称解析
- c# - 使用 C# 在 Visual Studio 或 VSCode 中未出现错误
- graphql - 除了 Apollo,还有其他 GraphQL Federation 实现吗?
- file-upload - iOS Safari 13.3.1 上传的文件大小为 0
- javascript - 确保 Javascript 中顺序命令的最有效方法
- c# - 启动浏览器时 PuppeteerSharp 抛出 ChromiumProcessException “无法创建连接”
- python - Python - 如何按非字母数字拆分字符串,但同时保留任何非字母数字
- python - DRF:如何在另一个类方法中调用一个类?