从通知发送消息和标题,以在活动的textview中显示

问题描述 投票:-1回答:1

我已成功使用我自己的服务器向Android应用程序发送Firebase通知,我在textview中显示通知(消息和标题)或在其他活动中显示Listview >>我需要帮助才能做到这一点

FcmMessagingService类:

public class FcmMessagingService extends FirebaseMessagingService {
private Map<String, String> data;
private static final String TAG = "MyFirebaseMsgService";

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    data = remoteMessage.getData();
    String message = data.get("message");
    String titledata = data.get("title");
    ManualNotification(titledata, message);
}

@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private void ManualNotification(String title, String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("message", messageBody);
    intent.putExtras(bundle);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher_background);
    Notification.BigPictureStyle bigpicture = new Notification.BigPictureStyle();
    bigpicture.bigPicture(bmp);
    NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle(title)
            //.setContentText(messageBody)
            .setLargeIcon(bmp)
            .setContentIntent(pendingIntent)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))
            .setContentText(messageBody).setLights(Color.YELLOW, 300, 300)
            .setVibrate(new long[]{100, 250})
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true);
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

}

android android-layout listview firebase
1个回答
0
投票

你应该看一下广播接收器来自你的服务onMessageReceived();

Intent in = new Intent("intentKey);
in.putExtra("key", notificationString);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(in);

在您的活动中注册onCreate()中的接收器

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
        mMessageReceiver, new IntentFilter("intentKey"));

并且出于onCreate()这段代码。

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
    String messageNotification = intent.getStringExtra("key");
    tvStatus.setText(messageNotification);
}
};
© www.soinside.com 2019 - 2024. All rights reserved.