简体   繁体   English

从 php web 应用程序发送推送通知以响应本机应用程序

[英]send push notification from php web app to react native app

I am having a php(CI) web app as a backend and react native as front.I want to send push notification to the react native app whenever, the admin will perform any action in backend the front should get the notification.我有一个 php(CI) web 应用程序作为后端,并将本机反应作为前端。我想在任何时候向反应本机应用程序发送推送通知,管理员将在后端执行任何操作,前端应该收到通知。 I have used firebase and implemeted local notification in front(RN) and working fine but I want do it remotely from backend.我已经使用了 firebase 并在前端(RN)实现了本地通知并且工作正常,但我想从后端远程进行。

How can I achieve this?我怎样才能做到这一点? any refferences will be helpfull I have spent almost a week and not get any appropriate solution.任何参考都会有所帮助我已经花了将近一周的时间并没有得到任何合适的解决方案。

You need to use Firebase Cloud Messaging (FCM) in order to send notification to your app from backend.您需要使用 Firebase 云消息 (FCM) 以便从后端向您的应用发送通知。

For backend you can send request to FCM using CURL, you can see some examples of code here How can I send a Firebase Cloud Messaging notification without use the Firebase Console?对于后端,您可以使用 CURL 向 FCM 发送请求,您可以在此处查看一些代码示例How can I send a Firebase Cloud Messaging notification without using the Z035489FF8D092741943E4A83241F Console? . .

In order to get notification sent from backend in app frontend you need again to use FCM api for react-native.为了在应用程序前端获得从后端发送的通知,您需要再次对 react-native 使用 FCM api。 Here's documentation for that https://rnfirebase.io/messaging/usage .这是https://rnfirebase.io/messaging/usage的文档。

You need to subscribe to messaging().onMessage service and then read your messages from there.您需要订阅 messages().onMessage 服务,然后从那里阅读您的消息。 Simple example will looks like (you can find that in rnfirebase docs)简单的示例看起来像(您可以在 rnfirebase 文档中找到)

import React, { useEffect } from 'react';
import { Alert } from 'react-native';
import messaging from '@react-native-firebase/messaging';

function App() {
  useEffect(() => {
    const unsubscribe = messaging().onMessage(async remoteMessage => {
      Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
    });

    return unsubscribe;
  }, []);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM