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

时间:2018-03-02 21:16:47

标签: android android-layout listview firebase

我已成功使用自己的服务器向Android应用程序发送Firebase通知,My i在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());
}

}

1 个答案:

答案 0 :(得分: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);
}
};