如何在 WearOs 上设置 firebase 通知

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

我正在构建一个应用程序并希望在移动应用程序和可穿戴应用程序上接收 firebase 通知,这两个应用程序具有相同的包名。我在移动应用和穿戴应用中都使用了 FirebaseMessagingService 类。移动应用成功收到通知,而穿戴应用未收到任何通知。我也谷歌但没有找到任何相关的解决方案。

任何帮助将不胜感激。

谢谢!

android push-notification firebase-cloud-messaging wear-os
2个回答
0
投票

基本上,Wear OS 只能显示通知,不能接收通知。链接设备的 Android 操作系统负责接收通知,并在显示通知时自动复制要在 Wear OS 设备上显示的通知。为避免重复,您应该明确 bridge 通知到 Wear OS 设备。

此外,Wear OS 可以自行构建和显示通知以响应某些触发事件或仅通过计时器等。

由于您的问题缺少实施细节,我唯一能为您提供的是理论/文档数据 - 不过,我认为您将能够解决您的问题。

更多信息在这里.


0
投票

在移动应用程序的 FirebaseMessagingService 实现中,使用 Wearable.getMessageClient() 方法向连接的 Wear OS 设备发送消息。例如:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // Forward the message to the connected Wear OS device
    String message = remoteMessage.getData().get("message");
    if (message != null) {
        Wearable.getMessageClient(this).sendMessage("my-node-id", "my-path", message.getBytes());
    }
}

在 Wear OS 应用程序的 WearableListenerService 实现中,使用 onMessageReceived() 方法来处理消息。例如:

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    // Show a notification on the Wear OS device
    if (messageEvent.getPath().equals("my-path")) {
        String message = new String(messageEvent.getData());
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setContentTitle("My App")
                .setContentText(message)
                .setSmallIcon(R.drawable.ic_notification)
                .setAutoCancel(true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        notificationManager.notify(0, builder.build());
    }
}

通过这些步骤,发送到移动应用程序的通知也应该转发到连接的 Wear OS 设备并显示为通知。

© www.soinside.com 2019 - 2024. All rights reserved.