javascript - 如何使用 react-native 实现推送通知
问题描述
每当应用程序处于后台或关闭时,Socket.io 通道都会断开连接,如何保持通道始终连接,因为我必须在我的聊天应用程序中包含推送通知功能。
解决方案
在 react-native 上处理通知的最佳方法是什么
这取决于您支持的平台,要在设备上接收移动推送通知,最好的方法是实施平台特定的解决方案。
你为什么这样做?
因为即使您的应用程序关闭或在后台,用户也会在屏幕上收到通知,您可以处理它
对于 iOS:
您已使用APNs服务器发送推送通知
对于安卓:
你必须使用GCM
为了使实现更容易,您可以使用以下服务:
如何用 react-native 实现它?
你有非常好的图书馆来做到这一点:
使用react-native-push-notification
这是这个库的一个例子:
var PushNotification = require('react-native-push-notification');
PushNotification.configure({
onRegister: function(token) {
// Call when your device register the token
// You have to pass this token to the API or services like OneSignal, Pusher etc...
console.log( 'TOKEN:', token );
},
// This is trigger when a remote notification is received or open
onNotification: function(notification) {
console.log( 'NOTIFICATION:', notification );
},
// This is only for Android
senderID: "YOUR GCM (OR FCM) SENDER ID",
// This is only for iOS
permissions: {
alert: true,
badge: true,
sound: true
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
// Ask the permission to receive notifications
requestPermissions: true,
});
您还拥有实现您选择的服务的库,例如:
- 推送器:react-native-pusher-push-notifications
- OneSignal:react-native-onesignal
- Firebase:react-native-firebase
希望我的回答对你有帮助
推荐阅读
- chart.js - 无法解析来自 json 的数据
- python - 有没有办法使用钩子在 PyTorch 中修改权重、梯度输出和激活输出?
- python - 是否可以使用字节大小拆分字符串变量
- .htaccess - 即使使用正确的凭据,htpasswd 也不会访问该站点
- javascript - 使用 JavaScript 从一个 HTML 页面获取输入并输出到另一个 HTML 页面
- css - sassc-rails 很难解析 css rgb 函数
- c++ - OpenImageDenoise - 在 C++ 中使用预编译头文件时架构 x86_64 的未定义符号
- python - 递归地找到所有可能的路径,在每个节点上具有一定数量的移动和不同的选项
- javascript - 自定义细化列表时 Algolia 和 VueJs 的计时问题
- python - 如何在 Python 中扫描特定子字符串的输入?