5. Navigating Push Notification Clicks

🚧

Before you begin, make sure you’ve completed these steps:

Android

IOS

šŸ‘

You've successfully completed the "Navigating Push Notification Clicks" step, if:

When clicking on a push notification, the developer console displays the link from that notification.

šŸ“˜

To receive the link in React Native, subscribe to the event generated in the native part upon click.

The subscription will be triggered with 2 arguments:

  • link is the link specified in the notification;
  • payload is the serialized payload of the push notification. If JSON is passed, it must be deserialized manually.
MindboxSdk.onPushClickReceived((pushUrl: String | null, pushPayload: String | null) => {
  console.log(`${pushUrl} ${pushPayload}`);
});

To trigger the subscription, the native part needs to pass the link.

Passing the Link from the iOS Part

In the ios/AppDelegate file, add the following call:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    // ...

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        Mindbox.shared.pushClicked(response: response)
        // Add the following code line
        MindboxJsDelivery.emitEvent(response)

        completionHandler()
    }
}
// MINDBOX INTEGRATION
@import Mindbox;
@import MindboxSdk;

@implementation AppDelegate
        
	...

// MINDBOX INTEGRATION
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  
	// Was before
	[[Mindbox shared] pushClickedWithResponse:response];

	// Should be added
	[MindboxJsDelivery emitEvent:response];
 
  completionHandler();
}
        
	...

@end

Passing the Link from the Android Part

In the android/MainActivity.java file, pass the following:

import android.content.Context
import android.content.Intent
import android.os.Bundle
import cloud.mindbox.mobile_sdk.Mindbox
import com.mindboxsdk.MindboxJsDelivery

class MainActivity : ReactActivity() {
    private var mJsDelivery: MindboxJsDelivery? = null

  ...
    
  private fun sendIntent(context: Context, intent: Intent) {
        Mindbox.onNewIntent(intent)
        Mindbox.onPushClicked(context, intent)
        mJsDelivery?.sendPushClicked(intent)
    }
  

		// Add
  private void initializeAndSentIntent(ReactContext context) {
    mJsDelivery = MindboxJsDelivery.Shared.getInstance(context)

    if (context.hasCurrentActivity()) {
       sendIntent(context, context.getCurrentActivity().getIntent())
    } else {
       sendIntent(context, this.getIntent())
    }
  }

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

		// Was before
		// sendIntent(this, intent);

		//	Replace with the following

    val mReactInstanceManager = getReactNativeHost().getReactInstanceManager()
    val reactContext = mReactInstanceManager.getCurrentReactContext()

    if (reactContext != null) {
           initializeAndSentIntent(reactContext);
       } else {
           // Add listener to initialize and send intent once React context is initialized
           mReactInstanceManager.addReactInstanceEventListener(object :
               ReactInstanceManager.ReactInstanceEventListener {
               override fun onReactContextInitialized(context: ReactContext) {
                   initializeAndSentIntent(context)
                   mReactInstanceManager.removeReactInstanceEventListener(this)
                }
            })
        }
    }

  override fun onNewIntent(intent: Intent) {
       super.onNewIntent(intent)
       sendIntent(this, intent)
   }
}
šŸ“˜

If you’re using the new architecture, rewrite the onCreate method as follows:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        getReactHost().addReactInstanceEventListener(object :
            ReactInstanceManager.ReactInstanceEventListener {
            override fun onReactContextInitialized(context: ReactContext) {
                initializeAndSentIntent(context)
            }
        })
    }
šŸ‘

You've successfully completed the "Navigating Push Notification Clicks" step, if:

When clicking on a push notification, the developer console displays the link from that notification.