香港专业教育学院成功实施了我的PHP应用程序服务器。我的Android设备可以接收数据有效载荷的通知。现在,我想与datapayload从我设备通知发送到一个特定的用户设备。火力地堡通知从Android设备
我的情况:访客来迎接员工接待员通知只有员工A。
我需要实现一个XMPP服务器?或者我的PHP服务器也可以处理这种机制? 我试图引用xmpp服务器https://github.com/carlosCharz/fcmxmppserver。但图书馆已被弃用。 任何帮助,将不胜感激
香港专业教育学院成功实施了我的PHP应用程序服务器。我的Android设备可以接收数据有效载荷的通知。现在,我想与datapayload从我设备通知发送到一个特定的用户设备。火力地堡通知从Android设备
我的情况:访客来迎接员工接待员通知只有员工A。
我需要实现一个XMPP服务器?或者我的PHP服务器也可以处理这种机制? 我试图引用xmpp服务器https://github.com/carlosCharz/fcmxmppserver。但图书馆已被弃用。 任何帮助,将不胜感激
所有您需要通知发送到特定的用户设备的设备令牌。
所以你可以通过两种方式做到这一点:
所以,你必须在服务器持有设备令牌。如果您收集这些信息与连接到用户的账户,你可以从一个用户传送到另一个
通知有一个code snippet从Android应用程序通过OkHttp库通知发送到concreate设备:
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());
}
});
你是在暗示我保存在设备上的服务器密钥? –
这总是一个选项。如果耀不想要它,从服务器发送 – mohax
是传请求你去那里..哪个服务器我为了实现到我的应用程序可以收到通知,同时也可以将通知发送给其他特定的设备? @mohax –
你不需要设备令牌。你不需要实现一个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);
}
基地网址:https://fcm.googleapis.com - 404所请求的URL /没有被此服务器上找到。
我推荐一个更简单的方法,强调这里:https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html –