我正在测试 react native PushNotificationIOS。
http://facebook.github.io/react-native/docs/pushnotificationios.html#content
我在 componentWillMount
函数中绑定(bind)如下事件
PushNotificationIOS.addEventListener('notification', this._onNotification);
我将推送通知从服务器发送到设备。 它无法捕获推送通知。
我只能在对象c代码下面收到推送通知
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
PushNotificationIOS
可以监听 RCTDeviceEventEmitter
调用。
但服务器通知无法监听。
有人知道这个问题吗?
最佳答案
推送通知不能开箱即用,React Native 文档中也没有记录这一点。您需要首先将一些内容添加到项目中才能连接通知。我从 github https://github.com/facebook/react-native/pull/1979#issue-94795697 上的一个未解决问题中找到了此信息.
您基本上需要在 AppDelegate.m 中手动连接通知,并从 RCTPushNotificationManager 调用相应的方法,以便 PushNotificationsIOS 类可以从您的 javascript 代码处理它们。
- 将 RCTPushNotification 添加到您的项目(并在build设置中链接二进制文件)。
- 将此 header 添加到 header 搜索路径:
$(SRCROOT)/node_modules/react-native/Libraries/**
- 将此代码添加到 AppDelegate.m:
#import "RCTPushNotificationManager.h"
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
[RCTPushNotificationManager application:application didFailToRegisterForRemoteNotificationsWithError:error];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
[RCTPushNotificationManager application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
[RCTPushNotificationManager application:application didReceiveRemoteNotification:notification];
}
关于push-notification - react native PushNotificationIOS不监听推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29586667/