我们使用GoogleCloudMessaging.getInstance(context).send(context.getString(R.string.gcm_defaultSenderId) + "@gcm.googleapis.com", mId, mBundle);
发送上游消息,但由于我尝试迁移新的fcm
概念,我也需要更改它,但还找不到任何文档。
我最好的猜测是使用:
RemoteMessage message = new RemoteMessage.Builder(<?>).setMessageId(mId).setData ...
FirebaseMessaging.getInstance().send(message);
但是什么是Builder作为参数?再一次,找不到api ......
如标题所述,如何使用新的fcm
概念发送上游消息?
答案 0 :(得分:10)
好吧,我的答案很快。保持问答,以备将来参考。我在https://firebase.google.com/docs/cloud-messaging/android/upstream#sample-send
找到了答案感谢谷歌智能排除搜索结果In order to show you the most relevant results, we have omitted some entries very similar to the 2 already displayed.
If you like, you can repeat the search with the omitted results included.
FirebaseMessaging fm = FirebaseMessaging.getInstance();
fm.send(new RemoteMessage.Builder(SENDER_ID + "@gcm.googleapis.com")
.setMessageId(Integer.toString(msgId.incrementAndGet()))
.addData("my_message", "Hello World")
.addData("my_action","SAY_HELLO")
.build());
答案 1 :(得分:0)
那么你可以从android应用程序直接将你的消息发送到android设备,这是我做过的简单实现,它对我很有用。
编译android排球库
compile 'com.android.volley:volley:1.0.0'
只需复制粘贴这个简单的功能;)你的生活就像黄油刀一样变得光滑。 :D
public static void sendPushToSingleInstance(final Context activity, final HashMap dataValue /*your data from the activity*/, final String instanceIdToken /*firebase instance token you will find in documentation that how to get this*/ ) {
final String url = "https://fcm.googleapis.com/fcm/send";
StringRequest myReq = new StringRequest(Request.Method.POST,url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(activity, "Bingo Success", Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(activity, "Oops error", Toast.LENGTH_SHORT).show();
}
}) {
@Override
public byte[] getBody() throws com.android.volley.AuthFailureError {
Map<String,String> rawParameters = new Hashtable<String, String>();
rawParameters.put("data", new JSONObject(dataValue).toString());
rawParameters.put("to", instanceIdToken);
return new JSONObject(rawParameters).toString().getBytes();
};
public String getBodyContentType()
{
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "key="+YOUR_LEGACY_SERVER_KEY_FROM_FIREBASE_CONSOLE);
return headers;
}
};
Volley.newRequestQueue(activity).add(myReq);
}
注意强> 如果您要向主题发送消息,则可以将参数 instanceIdToken 更改为 / topics / topicName 。 对于组,实现是相同的,但您只需要处理参数。 checkout Firebase documentation and you can pass those parameters. 如果您遇到任何问题,请与我们联系。