Google FCM POST请求发送广播消息并在后台处理它

时间:2017-07-19 19:23:55

标签: java android android-fragments notifications

我正在开发一个应用程序,当收到通知时,需要打开它,提取内部有效负载并将其放在CardView上 但是,我现在有两个问题,第一个是如何正确地执行POST请求,我需要另一个单独的应用程序吗?我的意思是,只写POST,还是有其他工具?

因为使用FCM仪表板,我只能发送

  

通知消息

虽然

  

数据讯息

还没有由FCM处理

第二个问题是如何在应用程序被杀或背景中处理通知。我已通过通知消息进行了验证,即使应用程序被终止或在后台运行,但onMessageReceived不会触发这些情况。

这是我的onMessageReceived

public class MessagingService extends FirebaseMessagingService  {

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Intent sendMessage = new Intent();
    sendMessage.setAction("com.blablabla.blablabla.MessaggingService");
    sendMessage.putExtra("REMOTE_PAYLOAD", remoteMessage.getData().toString()); 
    sendMessage.putExtra("REMOTE_DATA", remoteMessage.getNotification().getBody());
    sendBroadcast(sendMessage);
}

}

这就是我通过片段处理它的方式:(我需要片段,存在导航抽屉)

public class MainFragment extends Fragment {
TextView mTextView;

BroadcastReceiver mReceiver;
public class MessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        mTextView.setText(intent.getExtras().getString("REMOTE_DATA"));
    }

    public MessageReceiver(){

    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    LayoutInflater lf = getActivity().getLayoutInflater();

    View view =  lf.inflate(R.layout.fragment_main, container, false);
    mTextView = (TextView) view.findViewById(R.id.info_text);

    IntentFilter filter = new IntentFilter();
    filter.addAction("com.blablabla.blablabla.MessaggingService");
    mReceiver = new MessageReceiver();
    getActivity().registerReceiver(mReceiver, filter);

    return view;

}

}

0 个答案:

没有答案