安卓推送通知可以添加视频吗?

问题描述 投票:0回答:3

是否也可以像在 iOS 中那样将视频嵌入到 Android 丰富的通知中?我知道android官方仍然不支持这一点,但这可能是一些即兴发挥的棘手方法? :)

提前致谢。

java android push-notification android-remoteview android-push-notification
3个回答
3
投票

无法在 RemoteView 上显示视频。您只能使用官方文档

中描述的小部件

正如您在评论中显示的示例,它不是通知中的视频,而是一个简单的图像(但正如我假设的那样,当您单击它时,某些应用程序可以通过 URL(或类似的东西)显示视频,将开始并向您显示内容,仅此而已)


1
投票

您必须实施 FCM 消息“数据消息”。这将允许您发送自定义 json 数据。更多信息请参见:FCM 文档。此类消息不会自动显示 - 收到后您需要构建自定义通知布局。更多信息请参见:Android 开发文档 无法直接在通知面板上显示视频。看看 youtube 是如何做到的。


0
投票

使用视频链接创建通知负载: 从服务器或 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>
© www.soinside.com 2019 - 2024. All rights reserved.