react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[PushNotificationIOS] No register event ever gets triggered

Open timminkov opened this issue 11 years ago • 46 comments

I'm trying to get push notifications working on my device:

Pulling this commit on React 0.5.0.

Adding a PushNotificationIOS.requestPermissions() in my render pops open the permissions box, but when I add:

PushNotificationIOS.addEventListener('register', function(data) {
  console.log(data);
});

I get nothing logged. I've tried in the simulator and on an actual device but once I click the allow button, nothing. I've tried using this component too.

I also tried adding the following code to my AppDelegate.m from this thread:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [RCTPushNotificationManager application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

Any ideas?

timminkov avatar Jun 13 '15 07:06 timminkov

I just tried to do the same thing, but I didn't get it to work either.

I just wanted the device token so I could use it for the push notifications and my node server. I have found a working solution but it is without the register event. In #1019 the requestPermissions uses a callback for giving you the device token. I have used https://github.com/lazaronixon/react-native/commit/af08fde42e4fb57bb04b563756ac92e4d285ae85 and got it working. (It's only implemented in the SampleApp)

The commit didn't work right away but when I copied the AppDelegate+notification.m and the AppDelegate+notification.h from SampleApp/IOS to Libraries/PushNotificationIOS and added the PushNotificationIOS Library I got it working. (I am not sure that this is a right way of fixing it, I just started to play with IOS and Objective-C yesterday)

I hope it helped and looking forward for a proper fix.

DannyvanderJagt avatar Jun 13 '15 17:06 DannyvanderJagt

I tried copying over the two notification files to my PushNotificationIOS directory and adding it to my project, but still no dice.

timminkov avatar Jun 14 '15 11:06 timminkov

I'm sorry for my stupid answer yesterday. I didn't read properly and I just started with react and IOS two days ago. However I did some more digging and I think I have found a proper solution.

The problem is that didRegisterForRemoteNotificationsWithDeviceToken in the file RCTPushNotificationManager never gets called. I downloaded the latest version of react-native from the master and as suggested in https://github.com/facebook/react-native/pull/1304 I added this to the bottom of the AppDelegate.m file:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  [RCTPushNotificationManager application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

And this line to the top of AppDelegate.m:

#import "../../../Libraries/PushNotificationIOS/RCTPushNotificationManager.h"

But I also added this line to AppDelegate.h:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken;

When I added those line to my code the didRegisterForRemoteNotificationsWithDeviceToken did get called. But it then this error showed up: registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later. RCTPushNotificationManager.m

To fix this I changed this in the RCTPushNotificationManager.m file on line 140:

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0

into:

#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0

The register event in javascript is now working here and I get the device token as an argument. However I discovered that there is no error handling in the PushNotifications Library, all the errors are going away without notice. I think this should be fixed. Do make sure that all your certificates are valid etc.

I hope this is helped.

  • EDIT: I have forked the react-native repo and implemented these lines + error handling. The SampleApp does now have a working register event and an error event for catching errors. My fork: https://github.com/DannyvanderJagt/react-native

DannyvanderJagt avatar Jun 14 '15 14:06 DannyvanderJagt

@DannyvanderJagt were you able to get the event to trigger in the simulator?

timminkov avatar Jun 14 '15 19:06 timminkov

@timminkov The register event can't be fired in the simulator because remote notifications are not supported in the simulator. Remote/Push notifications can only be used on an actual device. The error event from my fork does fire on the simulator and if your run the app in the simulator it will give you this error: remote notifications are not supported in the simulator which is an actual IOS (Objective-C) error.

DannyvanderJagt avatar Jun 14 '15 20:06 DannyvanderJagt

Unfortunately following your instructions (and your commit) I'm STILL unable to even get the remote notifications are not supported in the simulator error message.

timminkov avatar Jun 15 '15 19:06 timminkov

I got this working by doing the following:

  1. Go to Build Settings in Xcode and under "Header Search Paths" add $(SRCROOT)/node_modules/react-native/Libraries/**
  2. At the top of AppDelegate: #import "RCTPushNotificationManager.h"
  3. Also in AppDelegate, add:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
  [RCTPushNotificationManager application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
  1. In RCTPushNotificationManager, around line 141, comment out the #if #else #endif and replace it with:
  if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { // iOS 8
    id notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
  }
  else {
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
  }

The #if #else macro is checking the minimum supported version for your project. This is a bad check (obviously), because if you are supporting iOS 7 but running on iOS 8, it will call the wrong code. respondsToSelector is the correct way to handle this.

After these steps, I finally got the push notifications popup when calling PushNotificationIOS.requestPermissions();

marcshilling avatar Jun 18 '15 20:06 marcshilling

Looks good and it is working here! A few days ago I fixed this locally but your way is cleaner. To make your code work on the SampleApp I needed to change the "Header Search Paths" to $(SRCROOT)/../../Libraries (recursive)

How are we going to implement this in the main react branch? Because before this fix we could just add the RCTPushNotificationManager to xcode without adding code to the AppDelegate. Is this something that just needs to be added to the docs or is there another way/better way of solving this bug?

Also I do think that the register event, which gives you the device token, and the error event should be implemented. Should this be in the same pull request/issue or should this be separate? (I just started to learn how to contribute to big public repositories.)

DannyvanderJagt avatar Jun 20 '15 14:06 DannyvanderJagt

I created a new fork of the react repo and added the changes from @marcshilling. I also added in another commit the error event. (Let me know if I should create a new issue for this)

The error event can be used just like this:

PushNotificationIOS.addEventListener('error', function(error){
  console.log(error); // {"NSLocalizedDescription":"no valid 'aps-environment' entitlement string found for application"}
});

At this point the event gives you the original IOS error, which exists of a error type and the message. So the output at this point in javascript can be something like: {"NSLocalizedDescription":"no valid 'aps-environment' entitlement string found for application"}

Should I leave it this way or is it better to separate the key and the error message? This way the key and the message are separated. Personally I prefer this.

PushNotificationIOS.addEventListener('error', function(key, error){
  console.log(key, error); // NSLocalizedDescription, no valid 'aps-environment' entitlement string found for application
});

DannyvanderJagt avatar Jun 20 '15 15:06 DannyvanderJagt

I tried to implement the above fixes using 0.6.0 with no luck. Can any one confirm that y'all are able to get the device token?

Basically the token event still isn't firing it seems.

Thanks

~DFO~

dorthwein avatar Jul 03 '15 22:07 dorthwein

@dorthwein I just downloaded the new 0.6.0 release and implemented the fix. It is working here and the register event gives the device token as a param. Note: This event will only be fired on an actual device and not in a simulator also all the certificates should be available and correct.

I will update (in a few hours) the 0.6-stable branch on my fork with this fix and the the error event I created. This way you will get an error message with the actual IOS error, maybe it helps us the figure this out.

DannyvanderJagt avatar Jul 04 '15 09:07 DannyvanderJagt

I have updated my fork on my fork. The register event and the error event are both working. The fixes are only implemented in examples/SampleApp for now!

In javascript I used:

PushNotificationIOS.requestPermissions();
PushNotificationIOS.addEventListener('register', function(devicetoken){
  console.log('register', devicetoken);
  alert('register: ' + devicetoken);
});

PushNotificationIOS.addEventListener('error', function(error){
  var errorKey = Object.keys(error)[0]; 
  var errorValue = error[errorKey];
  console.log('error', errorKey, errorValue);
  alert('error: ' + errorValue);
});

The error event is working but it can be cleaned up. I personally prefer the parameters to be: key, value instead of error but I would like to get thoughts from other people on this. Maybe it is a smart thing to start a separate issue/pull request for this after this one is solved and merged.

I hope this helps. Would love the hear if this is working for you or what isn't.

DannyvanderJagt avatar Jul 04 '15 11:07 DannyvanderJagt

@DannyvanderJagt awesome thanks - with this do you still need to do the above objective-c changes? Testing it today.

dorthwein avatar Jul 06 '15 15:07 dorthwein

@dorthwein If you use my fork then you don't need the above changes. Everything should be ready to go (only the SampleApp for now, because of the appDelegate changes). Let me know if it works for you.

DannyvanderJagt avatar Jul 06 '15 19:07 DannyvanderJagt

Does anyone know which AppDelegate files I should change to get a pull request accepted? I can't find any AppDelegate that is not part of an example. Thanks.

DannyvanderJagt avatar Jul 06 '15 19:07 DannyvanderJagt

@DannyvanderJagt your solution/branches worked great once I got my NPM stuff in order.

Thanks again!

dorthwein avatar Jul 06 '15 20:07 dorthwein

@DannyvanderJagt I looked around a bit for where to contribute to AppDelegate - No luck but I feel like it would be in the react-native-cli code since its the CLI Init that creates that initial file.

Any who here is the NPM link - I suggest reaching out to one of the contributors listed on there.

https://www.npmjs.com/package/react-native-cli

dorthwein avatar Jul 06 '15 20:07 dorthwein

I wanted to add another tidbit thats really important. Basically you need to add the corresponding bridge to AppDelegate.m for each JS call you want to use. So if you want to be able to receive notifications you also have to add

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)notification
{
  [RCTPushNotificationManager application:application didReceiveRemoteNotification:notification];
}

dorthwein avatar Jul 07 '15 14:07 dorthwein

Thank you, I will add this to my fork. I am planning to take a good look at this issue and create a pull request tomorrow.

DannyvanderJagt avatar Jul 07 '15 15:07 DannyvanderJagt

I have updated my fork. All the changes are implemented now and everything is working! There is one thing left to do. When some uses my fork without the RCTPushNotification library, a few errors occur because not everything can be found.

I think that there are two solutions for this:

  • Let the user add the code to the AppDelegate file when he/she wants to use push notifications. (This solution is used by phonegap/cordova and a lot of other plugins)
  • Or find a way to check when the RCTPushNotification library is available.

I would prefer the second solution, but I don't know if it is possible. I have looked around and tried some stuff but I can't get this to work.

I have tried to use this but this doens't work for me:

#if __has_include("RCTPushNotificationManager.h")
  // Pushnotification code.
#endif

Does anyone know a solution for this?

DannyvanderJagt avatar Jul 10 '15 13:07 DannyvanderJagt