来自 Android 个应用程序的 Firebase 云消息传递通知
Firebase Cloud Messaging Notification From Android App
我想从一个 android 设备向多个 android 设备发送通知,我正在使用 FCM 发送通知,但问题是我没有收到任何东西。我遵循了一些教程以及 Whosebug 上的一些链接,但我不明白我做错了什么。我尝试使用改造和 okhttp 发送通知,但我似乎无法生成通知。我可以从 Firebase 控制台生成通知,但不能从 android app.
使用改造
改装代码
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://fcm.googleapis.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
API api = retrofit.create(API.class);
PushNotificationModel pushNotificationModel = new PushNotificationModel(
"/topics/Receive",
new NotificationModel(
"Mobile",
"I am ready for you"
)
);
Call<PushNotificationModel> call = api.sendNotification(pushNotificationModel);
call.enqueue(new Callback<PushNotificationModel>() {
@Override
public void onResponse(Call<PushNotificationModel> call, Response<PushNotificationModel> response) {
Log.e(TAG, "onResponse: Code: " + response.code() /*+ " Response: " + new Gson().toJson(response)*/);
}
@Override
public void onFailure(Call<PushNotificationModel> call, Throwable t) {
Log.e(TAG, "onFailure: " + t.getMessage());
}
});
POJO类
public class PushNotificationModel {
private String to;
private NotificationModel notification;
public PushNotificationModel(String to, NotificationModel notification) {
this.to = to;
this.notification = notification;
}
}
public class NotificationModel {
private String title, body;
public NotificationModel(String title, String body) {
this.title = title;
this.body = body;
}
}
界面
public interface API {
@Headers({
"Authorization:key=AAAAO-53MSs:APA91bFR...JNL7GjX1D",
"Content-Type:application/json"
})
@POST("fcm/send")
Call<PushNotificationModel> sendNotification(@Body PushNotificationModel pushNotificationModel);
}
解决方案
我能够通过清理和重建项目来解决问题,有时在项目的第一次初始部署时我无法收到通知,因此关闭应用程序,从多任务栏中删除并重新打开应用程序解决了我能够正确接收通知的问题 :)。不管你使用什么凌空抽射或改装都适用。
下面的代码将向订阅名为 topicName
的主题的一组设备发送通知。
//send notification to other user
public void sendNotificationToUser() {
JSONObject mainObj = new JSONObject();
try {
mainObj.put("to", "/topics/" + topicName); // topicName = your topic name
JSONObject notificationObj = new JSONObject();
notificationObj.put("title", "Add your title");
notificationObj.put("body", "Body section");
mainObj.put("notification", notificationObj);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send",
mainObj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> header = new HashMap<>();
header.put("content-type", "application/json");
header.put("authorization", "key="+key);//key = your Database Key
return header;
}
};
requestQueue.add(jsonObjectRequest);
//
} catch (Exception ignored) {
}
}
//send notification to other user above
要接收通知,请将此 FCMService.class
添加到您的项目中
public class FCMService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
//message is recieved. Do whatever you want to do
}
@Override
public void onNewToken(@NonNull String s) {
FirebaseMessaging.getInstance().subscribeToTopic("your topic name here").addOnSuccessListener(new OnSuccessListener<Void>() {//subcribe again if some error occured
@Override
public void onSuccess(Void aVoid) {
}
});
super.onNewToken(s);
}
}
别忘了在清单中注册。在 manifest>application
中添加以下代码
<service
android:name=".FCMService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
在应用程序源代码中添加 firebase-key
不是一个好主意。您可以使用Firebase Cloud function
[必须升级项目才能使用功能]
我想从一个 android 设备向多个 android 设备发送通知,我正在使用 FCM 发送通知,但问题是我没有收到任何东西。我遵循了一些教程以及 Whosebug 上的一些链接,但我不明白我做错了什么。我尝试使用改造和 okhttp 发送通知,但我似乎无法生成通知。我可以从 Firebase 控制台生成通知,但不能从 android app.
使用改造
改装代码
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://fcm.googleapis.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
API api = retrofit.create(API.class);
PushNotificationModel pushNotificationModel = new PushNotificationModel(
"/topics/Receive",
new NotificationModel(
"Mobile",
"I am ready for you"
)
);
Call<PushNotificationModel> call = api.sendNotification(pushNotificationModel);
call.enqueue(new Callback<PushNotificationModel>() {
@Override
public void onResponse(Call<PushNotificationModel> call, Response<PushNotificationModel> response) {
Log.e(TAG, "onResponse: Code: " + response.code() /*+ " Response: " + new Gson().toJson(response)*/);
}
@Override
public void onFailure(Call<PushNotificationModel> call, Throwable t) {
Log.e(TAG, "onFailure: " + t.getMessage());
}
});
POJO类
public class PushNotificationModel {
private String to;
private NotificationModel notification;
public PushNotificationModel(String to, NotificationModel notification) {
this.to = to;
this.notification = notification;
}
}
public class NotificationModel {
private String title, body;
public NotificationModel(String title, String body) {
this.title = title;
this.body = body;
}
}
界面
public interface API {
@Headers({
"Authorization:key=AAAAO-53MSs:APA91bFR...JNL7GjX1D",
"Content-Type:application/json"
})
@POST("fcm/send")
Call<PushNotificationModel> sendNotification(@Body PushNotificationModel pushNotificationModel);
}
解决方案
我能够通过清理和重建项目来解决问题,有时在项目的第一次初始部署时我无法收到通知,因此关闭应用程序,从多任务栏中删除并重新打开应用程序解决了我能够正确接收通知的问题 :)。不管你使用什么凌空抽射或改装都适用。
下面的代码将向订阅名为 topicName
的主题的一组设备发送通知。
//send notification to other user
public void sendNotificationToUser() {
JSONObject mainObj = new JSONObject();
try {
mainObj.put("to", "/topics/" + topicName); // topicName = your topic name
JSONObject notificationObj = new JSONObject();
notificationObj.put("title", "Add your title");
notificationObj.put("body", "Body section");
mainObj.put("notification", notificationObj);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,"https://fcm.googleapis.com/fcm/send",
mainObj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> header = new HashMap<>();
header.put("content-type", "application/json");
header.put("authorization", "key="+key);//key = your Database Key
return header;
}
};
requestQueue.add(jsonObjectRequest);
//
} catch (Exception ignored) {
}
}
//send notification to other user above
要接收通知,请将此 FCMService.class
添加到您的项目中
public class FCMService extends FirebaseMessagingService {
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
//message is recieved. Do whatever you want to do
}
@Override
public void onNewToken(@NonNull String s) {
FirebaseMessaging.getInstance().subscribeToTopic("your topic name here").addOnSuccessListener(new OnSuccessListener<Void>() {//subcribe again if some error occured
@Override
public void onSuccess(Void aVoid) {
}
});
super.onNewToken(s);
}
}
别忘了在清单中注册。在 manifest>application
<service
android:name=".FCMService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
在应用程序源代码中添加 firebase-key
不是一个好主意。您可以使用Firebase Cloud function
[必须升级项目才能使用功能]