我成功实现了我的 php应用服务器。我的Android设备可以接收带有数据有效负载的通知。现在,我想通过datheayload从设备向特定用户设备发送通知。
我的案例:访客来见员工接待只通知员工。
我是否需要实施xmpp服务器?或者我的php服务器也可以处理这种机制? 我尝试引用xmpp服务器https://github.com/carlosCharz/fcmxmppserver。但是库被弃用了。 任何帮助将不胜感激
答案 0 :(得分:0)
基本网址:https://fcm.googleapis.com - 404在此服务器上找不到请求的网址/。
答案 1 :(得分:-1)
您需要向特定用户设备发送通知的只是设备令牌。
所以你可以用两种方式做到这一点:
所以你必须在服务器上持有设备令牌。如果您通过与用户帐户的连接收集此信息,则可以将通知从一个用户发送到另一个用户
有一个code snippet通过OkHttp库从android应用程序发送通知设备:
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
//in JSON field "to" is target device token.
//you can get it so: FirebaseInstanceId.getInstance().getToken();
//but, ofcause, you must get it from i.e. server
String json = "{
\"notification\": {
\"title\": \"news\",
\"text\": \"newsTExt\",
\"click_action\": \"test\"
},
\"data\": {
\"keyname\": \"any value\"
},
\"to\" : \"dAQilNw:APA91IFd666h7WVSlAOyS-WraSrGv_IRZM\"
}"
String keyFromConsole = ...;//here is key from firebase console (Settings-prohect settings-CLOUD MESSAGING-server key)
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url("https://fcm.googleapis.com/fcm/send")
.addHeader("Authorization", "key=" + keyFromConsole)
.addHeader("ContentType", "application/json")
.post(body)
.build();
Response response = client.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override public void onResponse(Call call, Response response) throws IOException {
System.out.println(response.body().string());
}
});
答案 2 :(得分:-1)
您不需要设备令牌。您不需要实现xmpp协议。订阅设备到主题。使用Retrofit从一台设备向其他设备发送通知。
public void onClick(View view) {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request original = chain.request();
// Request customization: add request headers
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", "key=legacy server key from FB console"); // <-- this is the important line
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
httpClient.addInterceptor(logging);
OkHttpClient client = httpClient.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://fcm.googleapis.com")//url of FCM message server
.client(client)
.addConverterFactory(GsonConverterFactory.create())//use for convert JSON file into object
.build();
// prepare call in Retrofit 2.0
FirebaseAPI firebaseAPI = retrofit.create(FirebaseAPI.class);
//for messaging server
NotifyData notifydata = new NotifyData("Notification title","Notification body");
Call<Message> call2 = firebaseAPI.sendMessage(new Message("/topics/news", notifydata));
call2.enqueue(new Callback<Message>() {
@Override
public void onResponse(Call<Message> call, Response<Message> response) {
Log.d("Response ", "onResponse");
t1.setText("Notification sent");
}
@Override
public void onFailure(Call<Message> call, Throwable t) {
Log.d("Response ", "onFailure");
t1.setText("Notification failure");
}
});
}
的POJO
public class Message {
String to;
NotifyData notification;
public Message(String to, NotifyData notification) {
this.to = to;
this.notification = notification;
}
}
和
public class NotifyData {
String title;
String body;
public NotifyData(String title, String body ) {
this.title = title;
this.body = body;
}
}
和FirebaseAPI
public interface FirebaseAPI {
@POST("/fcm/send")
Call<Message> sendMessage(@Body Message message);
}