react-native:推送通知+解析
我目前正在开发一个小型应用程序项目,以便在iOS上学习并尝试使用react-native。 我有一些解析(parse.com)的经验,并希望在新的应用程序中集成解析。 目前,我没有问题,包括解析反应原生的js。 我可以使用帐户等登录。现在我需要向一定数量的用户(并非所有用户)发送推送通知。
我不明白的是推送通知应如何与react-native和parse一起使用。 通常,我会使用用户ID连接设备安装,然后将推送发送给一定数量的用户(这意味着具有相应安装的设备)。 本地反馈指南( https://facebook.github.io/react-native/docs/pushnotificationios.html#content )没有提到类似的内容。 即使它提供了解析指南作为参考,我也看不出我应该如何通过解析发送推送。 该指南也留下了许多需要的信息。 这些“听众”订阅的来源是什么? 我将从哪个服务器发送通知等?
据我所知,解析js无法读取当前安装。 我也不愿意将Parse iOS添加到项目中。 这感觉不自然,不应该是必需的事情,虽然它可以让我阅读当前的安装。 (但仍解析js无法注册该安装以订阅推送通知)。
在这一点上,我感到有点失落。 这条信息( https://news.ycombinator.com/item?id=9271687 )告诉我它应该可能以某种方式。 我只是想不通如何:(
希望有人可以帮助我。 一些建议将得到真正的赞赏。
编辑 :react-native现在默认实现此行为。 有趣的部分是register事件的事件监听器,它现在返回设备令牌。 程序现在很简单。 看看文档还看看JWindey的答案。 其中有一些非常重要的要点是实际触发事件所需要的。
经过一段时间和大量的尝试,我们今天想出了答案。 这是我们的解决方案,似乎工作得很好。
我们使用以下资源:
- 反应母语
- 解析-JS
- Parse Rest API
- react-native-remote-push( https://github.com/darylrowland/react-native-remote-push )
按照推送通知的解析指南( https://parse.com/tutorials/ios-push-notifications )进行正确设置(配置文件,证书等)。 稍后使用react-native-remote-push组件,您不必执行步骤5和6。
现在将react-native-remote-push添加到您的项目中。 我们不得不对代码做一些小的调整(主要是处理遗留的objC代码),但这可能取决于你自己的项目。
我们的项目有一些“起始页面”,每次打开应用程序时都会显示。 在此页面上,我们处理推送通知权限以及设备令牌的注册和推送通知的监听器。 我们的目标是模仿我们使用解析iOS SDK获得的相同行为。
我们需要先注册设备并订阅推送通道。 react-native-remote-push允许我们处理权限并接收设备令牌。 然后,我们继续使用此设备令牌通过Rest API注册此安装。 此代码是componentDidMount()调用的一部分。
var PushManager = require('./RemotePushIOS'); var registerInstallation = require('./Installation'); componentDidMount() { PushManager.requestPermissions(function(err, data) { if (err) { console.log("Could not register for push"); } else { registerInstallation({ "deviceType": "ios", "deviceToken": data.token, "channels": ["global"] }); } }); PushManager.setListenerForNotifications(this.receiveRemoteNotification); }
PushManager是react-native-remote-push所需的组件,registerInstallation是包含Rest API调用的函数。
/** * registers an installation * data should look like the following: * { * "deviceType": "ios", // or "android" * // if android is targeted set * // "pushType": "gcm", * // "GCMSenderId": "56712320625545", // whatever the later means * "deviceToken": "29e32a686fd09d053e1616cb48", * "channels": [ * "" * ] * }; * for more information visit: * https://www.parse.com/docs/rest#installations-uploading */ var registerInstallation = function(data) { var url = "https://api.parse.com"; url += "/1/installations"; fetch(url, { method: 'post', headers: { 'Accept': 'application/json', 'X-Parse-Application-Id': PARSE_APP_ID, 'X-Parse-REST-API-Key': PARSE_REST_KEY, 'Content-Type': 'application/json' }, body: JSON.stringify(data) }) .then(processStatus) .then(parseJson) .catch(error); }; module.exports = registerInstallation;
“processStatus”,“parseJson”和“error”只是一些处理API调用结果的小函数。 如有必要,我可以提供更多细节。 此function允许我们通过“数据”对象添加大量信息,例如用户ID,应用程序版本,解析版本等,就像您习惯使用iOS SDK一样。 我们现在只有一个基本的示例,但在此基础上应该很容易扩展。 这一步对我们来说非常重要,因为我们需要将每个安装与特定用户相关联。
您现在应该能够接收推送通知。 您可以在充当侦听器的“receiveRemoteNotification”函数中处理它们。 在react-native-remote-push组件的网站上提供了一个基本function。
我希望我能分享一些关于这个主题的见解。 如果我应该详细介绍某些部分,我很乐意添加更多信息。
我使用Parse + react-native组合进行了一些调查并使其正常工作。
您必须将Parse SDK(按照指南)添加到项目中,并链接所有必需的库。
不要忘记在第5点添加步骤: https : //parse.com/tutorials/ios-push-notifications 。
然后将RCTPushNotificationManager.h + m(来自react-native / Libraries)添加到项目中。 之后,在AppDelegate.m中添加以下内容:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { [[NSNotificationCenter defaultCenter] postNotificationName:@"RemoteNotificationReceived" object:self userInfo:userInfo]; }
应该这样做。
官方PushNotificationIOS有register
事件,我们可以从中获取令牌。 因此,有了@MrMetet的REST API,我可以将设备安装到Parse。
PushNotificationIOS.addEventListener('register', function(token){ registerInstallation({ "deviceType": "ios", "deviceToken": token, "channels": ["global"] }) });
- Objective-c中CoreData的自动索引
- 编程创build一个uicollectionview时使用自定义的init方法
- 什么是最低的iOS版本开发人员可以创build一个应用程序?
- Swift 2 AVAudioSession setCategory额外参数’错误’
- CVOpenGLESTextureCacheCreateTextureFromImage返回错误6683
- 无法validationopenssl公钥
- NSData dataWithContentsOfFile返回null
- 什么是导致这个核心数据输出的差异?
- NSURLSessionTaskauthentication挑战completionHandler和NSURLAuthenticationChallenge客户端