Min Cheol Park
Min Cheol Park

Reputation: 75

react native PushNotificationIOS doesn't listen push notification

I'm testing react-native PushNotificationIOS.

http://facebook.github.io/react-native/docs/pushnotificationios.html#content

I bind event like below in componentWillMount function

PushNotificationIOS.addEventListener('notification', this._onNotification);

and I send push notification from server to device. It doesn't catch push notification.

I only can received push notification below object c code

(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}

PushNotificationIOS can listen with RCTDeviceEventEmitter call. but notification from server can't listen.

Does anyone know this problem?

Upvotes: 7

Views: 4953

Answers (2)

Troy Watt
Troy Watt

Reputation: 888

Push notifications don't work out of the box and this is not documented on the React Native docs. There are a few things you need to add to your project first in order to wire up notifications. I found this information from an open issue on github https://github.com/facebook/react-native/pull/1979#issue-94795697.

You basically need to wire the notifications manually in AppDelegate.m and call the corresponding methods from the RCTPushNotificationManager so the PushNotificationsIOS class can handle them from your javascript code.

  1. add RCTPushNotification to your project (and also link binaries in build settings).
  2. add this header to Header Search Paths: $(SRCROOT)/node_modules/react-native/Libraries/**
  3. add this code to 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];
}

Upvotes: 7

Daryl Rowland
Daryl Rowland

Reputation: 11

I found this too so wrote a replacement module to handle receiving push notifications - https://github.com/darylrowland/react-native-remote-push

Upvotes: 1

Related Questions