Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16552

iOS react-native-push-notification: the notification event doesn't trigger

$
0
0

I need to receive data while receiving a push notification.

Everything works as it should except for the 'notification' event. I see a notification in the notification centre and when I click on it, the onNotification method is triggered, which returns the data.

AppDelegate.m

// Required to register for notifications- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{ [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];}// 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 *)userInfofetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];}// Required for the registrationError event.- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];}// IOS 10+ Required for localNotification event- (void)userNotificationCenter:(UNUserNotificationCenter *)centerdidReceiveNotificationResponse:(UNNotificationResponse *)response         withCompletionHandler:(void (^)(void))completionHandler{  [RNCPushNotificationIOS didReceiveNotificationResponse:response];  completionHandler();}// IOS 4-10 Required for the localNotification event.- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ [RNCPushNotificationIOS didReceiveLocalNotification:notification];}

JavaScript code

import PushNotificationIOS from '@react-native-community/push-notification-ios';import PushNotification from 'react-native-push-notification';...    constructor(props) {        super(props);        PushNotification.configure({            // (optional) Called when Token is generated (iOS and Android)            onRegister: function (token) {                console.log('TOKEN:', token);                // save user token            },            // (required) Called when a remote is received or opened, or local notification is opened            onNotification: function (notification) {                console.log('NOTIFICATION:', notification);                // process the notification                // (required) Called when a remote is received or opened, or local notification is opened                notification.finish(PushNotificationIOS.FetchResult.NoData);            },            // (optional) Called when Registered Action is pressed and invokeApp is false, if true onNotification will be called (Android)            onAction: function (notification) {                console.log('ACTION:', notification.action);                console.log('NOTIFICATION:', notification);                // process the action            },            // (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)            onRegistrationError: function (err) {                console.error(err.message, err);            },        });        PushNotificationIOS.requestPermissions().then((data) => {            console.log('requestPermissions', data);            PushNotificationIOS.getInitialNotification().then((payload) => {                if (payload) {                    console.log('getInitialNotification', payload);                }            });            PushNotificationIOS.addEventListener('register', (token) => {                console.log('MyAPNSTOKEN', token);            });            PushNotificationIOS.addEventListener('registrationError',                (token) => {                    console.log('registrationError', token);                },            );            PushNotificationIOS.addEventListener('notification', function (                notification,            ) {                console.log('notification', notification);            });        });

Any help please!


Viewing all articles
Browse latest Browse all 16552

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>