📅  最后修改于: 2023-12-03 14:41:12.730000             🧑  作者: Mango
Firebase云消息传递指的是通过Firebase Cloud Messaging(Firebase消息传递服务)来传递消息。Firebase云消息传递可以使您的应用程序在前台或后台时接收消息,并根据这些消息广播用户通知、执行定时任务或在应用程序中打开可以帮助用户更好的使用应用程序的操作。下面是一些Firebase云消息传递的关键特点:
Firebase云消息传递服务使得您可以轻松地向Android、iOS 和 Web 应用程序发送通知和消息。Firebase云消息传递使用XMPP协议(sent over FCM connection servers)来传递消息。由于这种处理非常效率高且具有弹性,因此Firebase云消息传递可以处理大量的同步和异步消息流量,以便快速分发通知和消息。同时它也是有保障的,因此它永远不会丢失消息。
Firebase CloudMessaging包含下列组件:
Firebase CloudMessaging SDK是用于发送和接收消息的客户端库,包括两个主要部分:
Firebase CloudMessaging服务API是用于向Android或iOS应用程序发送消息的API。使用Firebase CloudMessaging API可以发送消息给单个设备、共享同一组特征的设备或存储在Firebase剪贴板中的设备。
Firebase CloudMessaging Console是可通过Web访问的控制台界面,用于注册应用程序以接收消息,并用于发送消息。使用Firebase CloudMessaging Console也可以查看FMC的诊断信息。
使用Firebase CloudMessaging,您可以轻松地在Android或iOS应用程序中发送和接收消息。以下是关于Firebase云消息传递的一些重要概念和步骤:
以下是Firebase CloudMessaging的使用示例(以Android为例):
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use FirebaseJobDispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}
private void scheduleJob() {
// [START dispatch_job]
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("my-job-tag")
.build();
dispatcher.schedule(myJob);
// [END dispatch_job]
}
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}