Open CarloTamburrelli opened 1 year ago
iOS simulator not supported to Push notification. If you want to test you need to check on real iOS device.
iOS simulator not supported to Push notification. If you want to test you need to check on real iOS device.
Ok thanks for the reply, I'll take a real device.
Can you tell me if the request from the server and client side with react native I make is correct? Thanks
Guys i managed to get the push notification working finally (with a real device), for IOS it was necessary to add the "notification" field, and using it as the "data" for android, so:
{
"registration_ids": ["ejXQlECjCeI:APA91bE7oaUhaFnGyl77lFrySdEaWxocM0oj81uNezACX1wsZXiTyL4OYo5ssvFjjWYpFymMVyqBccboVcwTTW2rvykOmV_CABDM7rTIRCiJFl_9ngf7SrDSYoFouwNj69JSwlH....."],
"data": {
"title": "Breaking News",
"message": "New Story available."
},
"notification": { // only for ios devices
"title": "Breaking News",
"message": "New Story available."
},
"priority":"high"
}
This will trigger the onNotification
on react native like a charm!
Very important: I use a M1 MacBook Air and also for the simulator the push notification are working (not just real device). The only problem so was the missing notification
field in the request.
Guys i managed to get the push notification working finally (with a real device), for IOS it was necessary to add the "notification" field, and using it as the "data" for android, so:
{ "registration_ids": ["ejXQlECjCeI:APA91bE7oaUhaFnGyl77lFrySdEaWxocM0oj81uNezACX1wsZXiTyL4OYo5ssvFjjWYpFymMVyqBccboVcwTTW2rvykOmV_CABDM7rTIRCiJFl_9ngf7SrDSYoFouwNj69JSwlH....."], "data": { "title": "Breaking News", "message": "New Story available." }, "notification": { // only for ios devices "title": "Breaking News", "message": "New Story available." }, "priority":"high" }
This will trigger the
onNotification
on react native like a charm!Very important: I use a M1 MacBook Air and also for the simulator the push notification are working (not just real device). The only problem so was the missing
notification
field in the request. where should i add this notification? i am using firebase console, there is no option to add notification?
Hi, I'm totally lost. I can't receive push notifications via the IOS simulator but I can receive the notification in Android. Here are the steps I took:
import <React/RCTBridge.h>
import <React/RCTBundleURLProvider.h>
import <React/RCTRootView.h>
import
import <React/RCTAppSetupUtils.h>
import <UserNotifications/UserNotifications.h>
import
import <FBSDKCoreKit/FBSDKCoreKit.h>
if RCT_NEW_ARCH_ENABLED
import <React/CoreModulesPlugins.h>
import <React/RCTCxxBridgeDelegate.h>
import <React/RCTFabricSurfaceHostingProxyRootView.h>
import <React/RCTSurfacePresenter.h>
import <React/RCTSurfacePresenterBridgeAdapter.h>
import <ReactCommon/RCTTurboModuleManager.h>
import <react/config/ReactNativeConfig.h>
static NSString *const kRNConcurrentRoot = @"concurrentRoot";
@interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> { RCTTurboModuleManager _turboModuleManager; RCTSurfacePresenterBridgeAdapter _bridgeAdapter; std::shared_ptr _reactNativeConfig;
facebook::react::ContextContainer::Shared _contextContainer;
}
@end
endif
@implementation AppDelegate
// Required for the register event.
(void)application:(UIApplication )application didRegisterForRemoteNotificationsWithDeviceToken:(NSData )deviceToken { [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; } // Required for the notification event. You must call the completion handler after handling the remote notification.
(void)application:(UIApplication )application didReceiveRemoteNotification:(NSDictionary )userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler]; } // Required for the registrationError event.
(void)application:(UIApplication )application didFailToRegisterForRemoteNotificationsWithError:(NSError )error { [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error]; } // Required for localNotification event
(void)userNotificationCenter:(UNUserNotificationCenter )center didReceiveNotificationResponse:(UNNotificationResponse )response withCompletionHandler:(void (^)(void))completionHandler { [RNCPushNotificationIOS didReceiveNotificationResponse:response]; }
(BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions { [FIRApp configure]; [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; RCTAppSetupPrepareApp(application);
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
if RCT_NEW_ARCH_ENABLED
_contextContainer = std::make_shared();
_reactNativeConfig = std::make_shared();
_contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
_bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
endif
NSDictionary initProps = [self prepareInitialProps]; UIView rootView = RCTAppSetupDefaultRootView(bridge, @"nuovozizi", initProps);
if (@available(iOS 13.0, *)) { rootView.backgroundColor = [UIColor systemBackgroundColor]; } else { rootView.backgroundColor = [UIColor whiteColor]; }
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *rootViewController = [UIViewController new]; rootViewController.view = rootView; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible];
// Define UNUserNotificationCenter UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self;
return YES; }
//Called when a notification is delivered to a foreground app. -(void)userNotificationCenter:(UNUserNotificationCenter )center willPresentNotification:(UNNotification )notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge); }
/// This method controls whether the
concurrentRoot
feature of React18 is turned on or off. /// /// @see: https://reactjs.org/blog/2022/03/29/react-v18.html /// @note: This requires to be rendering on Fabric (i.e. on the New Architecture). /// @return:true
if theconcurrentRoot
feture is enabled. Otherwise, it returnsfalse
.(BOOL)concurrentRootEnabled { // Switch this bool to turn on and off the concurrent root return true; }
(NSDictionary )prepareInitialProps { NSMutableDictionary initProps = [NSMutableDictionary new];
ifdef RCT_NEW_ARCH_ENABLED
initProps[kRNConcurrentRoot] = @([self concurrentRootEnabled]);
endif
return initProps; }
if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
endif
}
if RCT_NEW_ARCH_ENABLED
pragma mark - RCTCxxBridgeDelegate
pragma mark RCTTurboModuleManagerDelegate
(Class)getModuleClassFromName:(const char *)name { return RCTCoreModulesClassProvider(name); }
(std::shared_ptr)getTurboModule:(const std::string &)name
jsInvoker:(std::shared_ptr)jsInvoker
{
return nullptr;
}
(std::shared_ptr)getTurboModule:(const std::string &)name
initParams:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return nullptr;
}
(id)getModuleInstanceFromClass:(Class)moduleClass
{
return RCTAppSetupDefaultModuleFromClass(moduleClass);
}
endif
@end
PS: is it sufficient to receive push notifications in the client part? Or i need to integrate another libs?
with this code I am able to take the user token:
Server side:
I use this token in the server part like this:
First, i make a request to https://iid.googleapis.com/iid/v1:batchImport to convert APNS token to FCM token (is also explained here: https://github.com/zo0r/react-native-push-notification/issues/1251#issuecomment-565373047):
response:
I use finally the registration token to send the push notification to the client:
response:
But, even if the the request was successfull, in the client side i never receive the push notification, never.
Where am I wrong? In which of these steps am I doing something wrong?