【问题标题】:send message and title from notification to display in textview in activity从通知发送消息和标题以显示在活动的文本视图中
【发布时间】:2018-03-02 21:16:47
【问题描述】:

我已经使用我自己的服务器成功地将 Firebase 通知发送到 android 应用程序,我在 textview 或 Listview 中的花药活动中显示通知(消息和标题)>> 我需要帮助来做到这一点

FmMessagingService 类:

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】:

    你应该看看广播接收器 从您的服务 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);
    }
    };
    

    【讨论】:

    • 请问你有教程吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多