我正在构建一个应用程序并希望在移动应用程序和可穿戴应用程序上接收 firebase 通知,这两个应用程序具有相同的包名。我在移动应用和穿戴应用中都使用了 FirebaseMessagingService 类。移动应用成功收到通知,而穿戴应用未收到任何通知。我也谷歌但没有找到任何相关的解决方案。
任何帮助将不胜感激。
谢谢!
在移动应用程序的 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 设备并显示为通知。