Firebase 云消息让您可以实时向用户发送消息。我建议你从谷歌官方文档中阅读它,然后实施它,以便当有人喜欢你的帖子时,你的服务器能够发送 JSON 消息来激活 Firebase Cloud Messaging。
通知有两种形式:
- 通知消息,有时被认为是“显示消息”。
这些由 FCM(firebase 云消息传递)SDK 处理
自动地。
- 数据消息,由客户端应用处理。
第一步:将 FCM SDK API 添加到您的项目中
第二步创建一个可以接收您的数据消息的服务
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "myTag";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
}
}
第三步:从该服务创建通知
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Someone liked your post")
.setContentText("Accept this answer if it is useful!");
第四步:通过向您的 Android 应用发送带有通知的 JSON 请求来处理服务器端的任何“点赞”:
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", //this specefies the device
"notification":{
"title":"Someone liked your post",
"body":"col"
}
"data": {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
"android":{
"ttl":"86400s",
"notification"{
"click_action":"OPEN_ACTIVITY_1"
}
},
}
}
在我的blog 上了解有关 Firebase 云消息传递的更多信息。