【问题标题】:Push notification to a specific user with React Native使用 React Native 向特定用户推送通知
【发布时间】:2020-09-08 11:14:03
【问题描述】:

我是移动开发新手,我选择 React native,但我想向特定用户发送远程推送通知。我可以使用这个库: react-native-push-notification 吗?有完整的教程吗?

【问题讨论】:

    标签: react-native push-notification


    【解决方案1】:

    是的,您可以使用这个库 https://github.com/evollu/react-native-fcm 将您的应用程序与 firebase 连接,然后您所要做的就是记录设备令牌,使您能够仅使用 firebase 推送此设备的通知

    【讨论】:

    • 感谢您的回答。没有教程吗?
    • 库自述文件部分描述了如何安装它并包含示例
    【解决方案2】:

    您必须保存每个用户的 UUID,然后您可以使用 axios 向这些用户发送推送通知。

    export const sendNotificationFirebaseAPI = async (
      token: string,
      title: string,
      body: string,
      data?: object,
    ) => {
      if (token != '') {
        const headers = {
          Authorization: `key=${GOOGLE_FCM_KEY}`,
          'Content-Type': 'application/json',
        }
    
        const bodyToSend = JSON.stringify({
          to: token,
          notification: {
            title,
            body,
          },
          data,
        })
        try {
          await axios({
            method: 'post',
            url: 'https://fcm.googleapis.com/fcm/send',
            headers: headers,
            data: bodyToSend,
          })
        } catch (err) {
          return { err }
        }
      }
    }
    

    希望对你有帮助!

    有关详细信息,请参阅 google firebase 文档:https://firebase.google.com/docs/cloud-messaging/http-server-ref

    【讨论】:

    • 请把你的答案具体化,显示代码示例等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    相关资源
    最近更新 更多