React-native-push-notification: Onclick on Notification

Created on 17 Apr 2018  路  8Comments  路  Source: zo0r/react-native-push-notification

Hello guys, what is the action when user tries to click on arrived notification? How to handle only click on the notification when app is live/foreground?
Simple case:
Click on notification and redirect user to another page in app. (or open app and do the same thing)
Thanks!

Stale

Most helpful comment

@chukitow unfortunately userInteraction work if a user clicks on Notif in background.
For handler on click event on iOS, you can register next handler:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
          didReceiveNotificationResponse:(UNNotificationResponse *)response
          withCompletionHandler:(void (^)(void))completionHandler {
   // The user launched the app.
   if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
     NSMutableDictionary *newUserInfo = [[NSMutableDictionary alloc]initWithDictionary:response.notification.request.content.userInfo];
     [newUserInfo setObject:[NSNumber numberWithBool:TRUE] forKey:@"userInteraction"];

     [RNCPushNotificationIOS didReceiveRemoteNotification:newUserInfo];
   }
}

and check this native event in your js event onNotification. For example:

onNotification = (notification) => {
  ...
  if (notification.data.userInteraction) {
    navigate(ROUTE_NAME);
  }
 ...
};

All 8 comments

+1 Would be so great, it's actually the only thing I need

+1

@zo0r or @Gp2mv3 any ideas if this is possible or something that is planned?

+1

Hey Guys! the notification has a boolean property called userInteraction, this means the user pressed on the notification.

onNotifictaion(notificication) {
  ....
  if(notification.userInteraction) {
    // Redirect wherever you want  
  }
}

Anyone get any solution to manage click event and redirect to a particular screen?

@chukitow unfortunately userInteraction work if a user clicks on Notif in background.
For handler on click event on iOS, you can register next handler:

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
          didReceiveNotificationResponse:(UNNotificationResponse *)response
          withCompletionHandler:(void (^)(void))completionHandler {
   // The user launched the app.
   if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
     NSMutableDictionary *newUserInfo = [[NSMutableDictionary alloc]initWithDictionary:response.notification.request.content.userInfo];
     [newUserInfo setObject:[NSNumber numberWithBool:TRUE] forKey:@"userInteraction"];

     [RNCPushNotificationIOS didReceiveRemoteNotification:newUserInfo];
   }
}

and check this native event in your js event onNotification. For example:

onNotification = (notification) => {
  ...
  if (notification.data.userInteraction) {
    navigate(ROUTE_NAME);
  }
 ...
};

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings