Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 559
Min Cheol Park Avatar asked Apr 12 '15 06:04

Min Cheol Park


2 Answers

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];
}
like image 158
Troy Watt Avatar answered Nov 13 '22 15:11

Troy Watt


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

like image 29
Daryl Rowland Avatar answered Nov 13 '22 14:11

Daryl Rowland