如何在活动类和非活动类之间发送数据



我想将数据从非活动类发送到MainActivity类,并使用AlertDialog向用户显示该数据。数据是来自Firebase的RemoteMessage。我想我可以使用一个界面,但我尝试了一些选项,但没有成功。如有任何帮助,我们将不胜感激。谢谢

我的非活动类

public class FcmMessageListenerService extends FirebaseMessagingService  {
private static final String NOTIFICATION_CHANNEL_ID = "CITYLINE_DEFAULT_NOTIFICATION_CHANNEL";
public static final String MANDATOR_ID_KEY = "mandator_id";
public static final String MACHINE_SERIAL_NUMBER_KEY = "serial_number";
private iDialogNotificationInterface myInterface;
private  Map<String, String> data;
private Intent openAppIntent;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
data = remoteMessage.getData();
String mandatorId = data.get("mandatorID");
String message = data.get("message");
String serialNumber = data.get("pmID");
if (message != null) {
Log.i("OnMessageReceived: ", message);
sendNotification(mandatorId, message, serialNumber);
myInterface.sendDialogNotification(mandatorId, message, serialNumber);
} else {
Log.e("OnMessageReceived", "null error");
}
}
@Override
public void onNewToken(String s) {
super.onNewToken(s);
SharedPrefsUtils.setBooleanValue(this, DEVICE_TOKEN_REFRESH_EVENT_KEY, true);
SharedPrefsUtils.removeValueByKey(this, DEVICE_PUSH_NOTIFICATION_TOKEN_KEY);
Intent intent = new Intent(this, DeviceRegistrationService.class);
startService(intent);
}
private void sendNotification(String mandatorId, String message, String serialNumber){
String notificationTitle = this.getString(R.string.app_name);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && notificationManager != null) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Default Notifications", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("Status Change Notification");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(false);
notificationManager.createNotificationChannel(notificationChannel);
}

if (SharedPrefsUtils.getStringValue(this, SharedPrefsUtils.SESSION_COOKIE_DATA_KEY).equals("")) {
openAppIntent = new Intent(this, LoginActivity.class);
} else {
openAppIntent = new Intent(this, MainActivity.class);
}
openAppIntent.putExtra(MANDATOR_ID_KEY, mandatorId);
openAppIntent.putExtra(MACHINE_SERIAL_NUMBER_KEY, serialNumber);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(this);
taskStackBuilder.addNextIntentWithParentStack(openAppIntent);
PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_cityline)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_cityline))
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentTitle(notificationTitle)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(resultPendingIntent)
.setAutoCancel(true);
int notificationId = SharedPrefsUtils.getIntValue(this, SharedPrefsUtils.NOTIFICATION_UNIQUE_ID_KEY);
if (notificationManager != null) {
notificationManager.notify(notificationId, notificationBuilder.build());
notificationId++;
if (notificationId > 100000)
notificationId = 0;
SharedPrefsUtils.setIntValue(this, SharedPrefsUtils.NOTIFICATION_UNIQUE_ID_KEY, notificationId);
}
}

String id = mandatorId;
String msg = message;
String serial = serialNumber;
Intent serviceIntent = new Intent(this, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
serviceIntent.putExtra("ID", id);
serviceIntent.putExtra("MSG", msg);
serviceIntent.putExtra("SERIAL", serial);

}

主要活动

private void openNotificationDialog( String mandatorId, String message, String serialNumber){
Log.i("RAZZ", getApplicationContext().toString());
String notificationTitle = this.getString(R.string.app_name);
infoDialog = mBuilder.create();
mBuilder.setCancelable(true);
infoDialog.setTitle(notificationTitle);
infoDialog.setMessage(message);
infoDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
infoDialog.dismiss();
}
});
infoDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
infoDialog.show();
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
@Override
public void sendDialogNotification(String id, String message, String serialNmb) {
openNotificationDialog(id, message, serialNmb);
}

界面

public interface iDialogNotificationInterface {
void sendDialogNotification(String id, String message, String serialNmb);

}

接口的用法如下:

public class A implements interfaceA{
private interfaceA dataMember;
public void setMember(interfaceA member)
{
dataMember = member;
}
// rest of class code
// when needed to notify interfacemember, you call dataMember.function()
}

在你的主活动中,你应该做一些类似的事情

FcmMessageListenerService obj.setMember(MainActivity.this)

从您的代码来看,您使用的似乎是服务,而不是类。由于这是一项服务,我会通过意向而不是接口将数据发送到MainActivity。

您可以使用Sharepreference将数据存储在Non-Activity类中,然后将数据提取到您的Activity类中,反之亦然。

在非活动类中

初始化

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

存储数据

editor.putBoolean("IsNotificationReceived", true); // Storing boolean as true when you receive the notifiaction also store the body or message into this sharedpref form the notification 
editor.commit(); // commit changes !Important line to save data into the sharedpreference

活动类

检索数据

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0);
boolean isNotificationReceived = pref.getBoolean(IsNotificationReceived, false);

在onResume或初始化类时检查isNotificationReceived,如果值为true,则调用对话框并显示相应的消息

打开对话框时,请确保将sharedpref isNotificationReceived编辑为false,否则无论是否收到,只要初始化类,对话框都会打开

相关内容

  • 没有找到相关文章

最新更新