【问题标题】:Push Notifications with RealmDB (MongoDB) and react-native-push-notification使用 RealmDB (MongoDB) 和 react-native-push-notification 推送通知
【发布时间】:2021-10-14 15:41:09
【问题描述】:

我正在将 Realm 用于一个已经非常复杂的应用程序,我想通过添加推送通知来完成它。我已经安装了 react-native-push-notification 并使其与 FCM(Firebase 云消息传递)一起使用。

在客户端,我确实收到了来自 Firebase 的测试通知。

然后,我通过添加 Sender Id 和 API 密钥在 Realm 控制台中配置推送通知。

问题是,当我从 Realm 发送通知时,它最终以“已发送”状态结束,但绝对没有任何反应,Realm 控制台中没有日志,Firebase 端没有任何内容,客户端也没有。

关于 Realm 推送通知的文档非常有限,我不知道该怎么做。文档主要针对IOS和Android。

这是我在 index.js 中的代码:

import PushNotificationIOS from '@react-native-community/push-notification-ios';
import PushNotification from 'react-native-push-notification';

PushNotification.configure({
  onRegister: function (token) {
    console.log('TOKEN:', token);
  },
  onNotification: function (notification) {
    console.log('NOTIFICATION:', notification);
    notification.finish(PushNotificationIOS.FetchResult.NoData);
  },

  onAction: function (notification) {
    console.log('ACTION:', notification.action);
    console.log('NOTIFICATION:', notification);
  },

  onRegistrationError: function (err) {
    console.error(err.message, err);
  },
  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },
  popInitialNotification: true,
  requestPermissions: true,
});

PushNotification.createChannel(
  {
    channelId: 'fcm_fallback_notification_channel',
    channelName: 'FCM CHANNEL',
    channelDescription: 'Description test',
  },
  created => console.log(`CreateChannel returned '${created}'`),
);

PushNotification.localNotification({
  channelId: 'fcm_fallback_notification_channel',
  vibrate: true,
  vibration: 300,
  playSound: true,
  soundName: 'default',
});

有人知道教程或可以给我一些额外信息的东西吗?

【问题讨论】:

  • 请谨慎寻求关于 SO 的教程。这通常会使问题结束,因为(来自 SO Docs)要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题对于 Stack Overflow 来说是无关紧要的,因为它们倾向于吸引固执己见的答案和垃圾邮件。也不清楚要问什么。如果您正在使用 Firebase 通知并且它正在工作,您还需要什么?您为什么不直接使用 Firebase 来实现该功能?
  • 感谢您的回答,我的数据库在 MongoDB 上并且正在使用 Realm,因此我需要在 RealmDB / MongoDB 中发送与我的数据库更改相关的通知。 Firebase Cloud Messaging 只是一种在需要时发送由 Realm 触发的推送通知的服务。并且在这里需要一个教程,因为完全不可能找到一个通过 React Native 在 Realm 和 Firebase 之间进行推送通知的教程......所以“垃圾邮件”或“有意见的答案”应该非常罕见。
  • 只是试图阻止问题被关闭。 Firebase 和 Realm 完全不相关,没有任何东西可以将它们联系在一起。您必须在 Realm 数据库中添加一个观察者,然后触发 Firebase 云函数来发布通知。但同样,这些是不相关的技术,您需要编写代码将它们链接在一起。
  • 请看我的回答。

标签: mongodb firebase react-native realm react-native-push-notification


【解决方案1】:

所以要让它工作,你必须在 index.js 中添加这一行:

PushNotification.subscribeToTopic('topic-id-in-realm-console');

并在 Realm 控制台中使用相同的主题 ID:

它最终是这样的(index.js):

import PushNotificationIOS from '@react-native-community/push-notification-ios';
import PushNotification from 'react-native-push-notification';

PushNotification.configure({
  onRegister: function (token) {
    console.log('TOKEN:', token);
  },

  onNotification: function (notification) {
    console.log('NOTIFICATION:', notification);
    notification.finish(PushNotificationIOS.FetchResult.NoData);
  },

  onAction: function (notification) {
    console.log('ACTION:', notification.action);
    console.log('NOTIFICATION:', notification);
  },

  onRegistrationError: function (err) {
    console.error(err.message, err);
  },

  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },
  popInitialNotification: true,
  requestPermissions: true,
});

PushNotification.subscribeToTopic('topic-id-in-realm-console');// <== HERE

在客户端:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 2019-10-03
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多