香港專業教育學院成功實施了我的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 –