package com.wonderpush.sdk;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;

import androidx.annotation.Nullable;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

public class WonderPushNotificationTrackingReceiver extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Track the notification click
        track(getIntent());

        // Exit as soon as possible
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);

        // Track the notification click
        track(getIntent());

        // Exit as soon as possible
        finish();
    }

    private void track(Intent intent) {
        NotificationModel notif = NotificationModel.fromLocalIntent(intent, this);
        if (notif != null) {
            NotificationManager.handleOpenedNotificationFromTrackingActivity(this, intent, notif);
            ActivityLifecycleMonitor.onNextResume(activity -> WonderPush.showPotentialNotification(activity, intent));

            Uri parsedTargetUrl = Uri.parse(notif.getTargetUrl());
            if (WonderPush.INTENT_NOTIFICATION_SCHEME.equals(parsedTargetUrl.getScheme())
                    && WonderPush.INTENT_NOTIFICATION_WILL_OPEN_AUTHORITY.equals(parsedTargetUrl.getAuthority())
                    && parsedTargetUrl.getPathSegments().size() == 1
                    && WonderPush.INTENT_NOTIFICATION_WILL_OPEN_PATH_BROADCAST.equals(parsedTargetUrl.getLastPathSegment())
            ) {
                // wonderpush://notificationOpen/broadcast
                // Broadcast locally that a notification is to be opened, and don't do anything else
                Intent notificationWillOpenIntent = new Intent();
                notificationWillOpenIntent.setPackage(getApplicationContext().getPackageName());
                notificationWillOpenIntent.setAction(WonderPush.INTENT_NOTIFICATION_WILL_OPEN);
                //notificationWillOpenIntent.setData(Uri.parse(WonderPush.INTENT_NOTIFICATION_WILL_OPEN_SCHEME + "://" + WonderPush.INTENT_NOTIFICATION_WILL_OPEN_AUTHORITY + "/" + WonderPush.INTENT_NOTIFICATION_WILL_OPEN_PATH_BROADCAST));
                notificationWillOpenIntent.putExtra(WonderPush.INTENT_NOTIFICATION_WILL_OPEN_EXTRA_RECEIVED_PUSH_NOTIFICATION,
                        (Parcelable) intent.getParcelableExtra("receivedPushNotificationIntent"));
                notificationWillOpenIntent.putExtra(WonderPush.INTENT_NOTIFICATION_WILL_OPEN_EXTRA_NOTIFICATION_MODEL,
                        // this extra must be removed if handled outside the app,
                        // or we'll get ClassNotFoundException: E/Parcel: Class not found when unmarshalling: com.wonderpush.sdk.Notification*Model
                        notif);
                notificationWillOpenIntent.putExtra(WonderPush.INTENT_NOTIFICATION_WILL_OPEN_EXTRA_NOTIFICATION_TYPE,
                        notif.getType().toString());
                notificationWillOpenIntent.putExtra(WonderPush.INTENT_NOTIFICATION_WILL_OPEN_EXTRA_FROM_USER_INTERACTION,
                        intent.getBooleanExtra("fromUserInteraction", true));
                notificationWillOpenIntent.putExtra(WonderPush.INTENT_NOTIFICATION_WILL_OPEN_EXTRA_AUTOMATIC_OPEN,
                        true);
                LocalBroadcastManager.getInstance(this).sendBroadcast(notificationWillOpenIntent);
            }
        }
    }

}

@@ -1220,24 +1220,118 @@
  intent));%0A%0A
+            String targetUrl = notif.getTargetUrl();%0A            if (targetUrl != null) %7B%0A    
             
@@ -1362,34 +1362,25 @@
 i.parse(
-notif.getT
+t
 argetUrl
 ());%0A   
@@ -1371,21 +1371,23 @@
 argetUrl
-()
 );%0A
+    
         
@@ -1480,32 +1480,36 @@
                 
+ 
+   
 && WonderPush.IN
@@ -1581,24 +1581,28 @@
 uthority())%0A
+    
             
@@ -1670,32 +1670,36 @@
                 
+    
 && WonderPush.IN
@@ -1798,20 +1798,28 @@
         
+   
+ 
 ) %7B%0A
+    
         
@@ -1885,16 +1885,20 @@
         
+  
+  
 // Broad
@@ -1990,16 +1990,20 @@
         
+    
 Intent n
@@ -2052,32 +2052,36 @@
                 
+ 
+   
 notificationWill
@@ -2153,32 +2153,36 @@
                 
+    
 notificationWill
@@ -2245,16 +2245,20 @@
 _OPEN);%0A
+    
         
@@ -2496,32 +2496,36 @@
                 
+   
+ 
 notificationWill
@@ -2643,16 +2643,20 @@
         
+    
 (Parcela
@@ -2730,32 +2730,36 @@
                 
+  
+  
 notificationWill
@@ -2845,16 +2845,20 @@
 _MODEL,%0A
+    
         
@@ -2955,16 +2955,20 @@
         
+    
 // or we
@@ -3107,24 +3107,28 @@
         
+    
 notif);%0A
         
@@ -3111,32 +3111,36 @@
         notif);%0A
+    
                 
@@ -3261,24 +3261,28 @@
             
+  
+  
 notif.getTyp
@@ -3294,24 +3294,28 @@
 oString());%0A
+    
             
@@ -3448,16 +3448,20 @@
         
+    
 intent.g
@@ -3514,32 +3514,36 @@
                 
+   
+ 
 notificationWill
@@ -3653,15 +3653,23 @@
     
+    
 true);%0A
+    
     
@@ -3755,32 +3755,50 @@
 illOpenIntent);%0A
+                %7D%0A
             %7D%0A  

Fix NPE when no targetUrl was given in the push notification