React native ios handle push notification without clicking on notification

Viewed 1525

I am using react-native 0.60, Firebase push notification. I need help with react native firebase ios push notification with data payload. I am able to send notifications with data payload. I am able to receive the notification when the app is foreground, background, killed. I am sending data with notification.

    {
      "to" : "FCM Token",
      "show_in_foreground" : "true",
      "collapse_key" : "type_a",
      "notification" : {
      "notification_type" : "N",
      "ad_type" : "banner",
      "sub_type" : "info",
      "text_message" : "Lorem ipsum dolor sit amet",
      "Title": "Notification 0000",
      "image_url": "https://firebasestorage.com/xyz",
      "ad_time" : 7,
      "x_time" : 4,
      "delay_time" : 4
    },
    "data" : {
      "notification_type" : "N",
      "ad_type" : "banner",
      "sub_type" : "info",
      "text_message" : "Lorem ipsum dolor sit amet",
      "Title": "Notification 0000",
      "image_url": "https://firebasestorage.googleapis.com/xyz",
      "ad_time" : 7,
      "x_time" : 4,
      "delay_time" : 4
    },
    "content-available" : true,
    "priority": "high"
  }

I want to show notification and also store data in local app database at the same time even if the user doesn't click on the notification.

I am able to handle the notification when user clicks on the notification. I want to update the data in the app when the notification is received by the user and also show the notification. I dont want to use silent background notification.

My Appdelegate code

   - (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo fetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{
     if ([[FIRAuth auth] canHandleNotification:userInfo]) {
        completionHandler(UIBackgroundFetchResultNoData);
        return;
     }
     [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
     [[RNFirebaseNotifications instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
     NSLog(@"Inactive - the user has tapped in the notification when app was closed or in background 3");

   }

I want to achieve 1. User should get the notification even if the app is closed, killed, background. 2. User's App local Database should be updated even if user doesnt click on the notification.

I have achieved the first point looking for 2nd. I am new to react native. I know that when the notification is received the completion handler method is called. when the app is killed nothing is showed in the xcode log. how do i write code in completion handler method ? and how do i check if the method is called and task is performed.

3 Answers
messaging().onNotificationOpenedApp(remoteMessage => {
      console.log(
        'Notification caused app to open from background state:',
        remoteMessage.notification,
      );
      navigation.navigate(remoteMessage.data.type);
    });

    // Check whether an initial notification is available 
    // This will handle notification when App is opened from killed state or background
    messaging()
      .getInitialNotification()
      .then(remoteMessage => {
        if (remoteMessage) {
          console.log(
            'Notification caused app to open from quit state:',
            remoteMessage.notification,
          );
          setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
        }
        setLoading(false);
      });

Use this notification listeners

2nd thing React-Native Does not can not manage kill state for Application. Because when Application is killed, js engine shuts down.

3rd: v6 lib does not support badge count updating like v5 did. So When there are notification and When User Directly Open Application all notification should be cleared, So that can be done from Native Side

For Android -> MainActivity.java

@Override
protected void onResume() {
super.onResume();

// Clear all notification
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
}

So This Will Clear All Notification From StatusBar, LockScreen, whenever app is opened whether directly or from Notification Press. But still do this if requirement is this.

On more thing Android manages badge count and update automatically, but for iOS Native Code implementation must. in AppDelegate file.

Most common mistake seen : remoteMessage.notification and remoteMessage.data both are different, console whole remoteMessage object :)))

So, in iOS currently, there are no background services that could be run on the background as we have it on Android. So, from my perspective, you can't check were notification shown or not to the user in realtime if the app was killed. I can't understand why you need that. Notifications act pretty much correctly, so, you don't need to check iOS is it works. What you can do is collect information about notifications after your app restarts.

 messaging().onNotificationOpenedApp(remoteMessage => {
  console.log(
    'Notification caused app to open from background state:',
    remoteMessage.notification,
  );
  YOUR_NAVIGATION_PATH_HERE;
});

SOURCE : https://rnfirebase.io/messaging/notifications

Related