簡體   English   中英

使用 RealmDB (MongoDB) 和 react-native-push-notification 推送通知

[英]Push Notifications with RealmDB (MongoDB) and react-native-push-notification

我正在將 Realm 用於已經非常復雜的應用程序,我想通過添加推送通知來完成它。 我已經安裝了 react-native-push-notification 並使其與 FCM (Firebase Cloud Messaging) 配合使用。

在客戶端,我確實收到來自 Firebase 的測試通知。

Firebase 控制台 客戶端

然后,我通過添加發件人 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',
});

有誰知道教程或可以給我一些額外信息的東西?

所以為了讓它工作,你必須在 index.js 中添加這一行:

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

並在 Realm 控制台中使用相同的主題 ID:

領域控制台主題 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

在客戶端:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM