是否也可以像在 iOS 中那样将视频嵌入到 Android 丰富的通知中?我知道android官方仍然不支持这一点,但这可能是一些即兴发挥的棘手方法? :)
提前致谢。
您必须实施 FCM 消息“数据消息”。这将允许您发送自定义 json 数据。更多信息请参见:FCM 文档。此类消息不会自动显示 - 收到后您需要构建自定义通知布局。更多信息请参见:Android 开发文档 无法直接在通知面板上显示视频。看看 youtube 是如何做到的。
使用视频链接创建通知负载: 从服务器或 Firebase 控制台发送通知时,请在视频链接中包含数据负载:
{
"to": "<FCM_TOKEN>",
"data": {
"title": "Your notification title",
"body": "Your notification message",
"videoUrl": "https://example.com/your_video.mp4"
}
}
在 Android 应用程序中处理通知:
if (remoteMessage.getData().containsKey("videoUrl")) {
String videoUrl = remoteMessage.getData().get("videoUrl");
Intent intent = new Intent(this, VideoPlayerActivity.class);
intent.putExtra("videoUrl", videoUrl);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("New Video Notification")
.setContentText("Tap to watch the video")
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, builder.build());
}
在 AndroidManifest.xml 中注册服务:
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>