目录
此内容是否有帮助?

# Android Push Integration Document

# FCM push

# 1.1 Report "Push ID"

  • Track the FCM Token after calling TA login or switching accounts.
//initialize TA SDK
ThinkingAnalyticsSDK instance = ThinkingAnalyticsSDK.sharedInstance(this, APPID, SERVER_URL);
instance.login("user_id");
FirebaseMessaging.getInstance().getToken()
    .addOnCompleteListener(new OnCompleteListener<String>() {
        @Override
        public void onComplete(@NonNull Task<String> task) {
          if (!task.isSuccessful()) {
            Log.w(TAG, "Fetching FCM registration token failed", task.getException());
            return;
          }

          // Get new FCM registration token
          String token = task.getResult();
          JSONObject properties = new JSONObject();
          properties.put("fcm_token",token);
          instance.user_set(properties);
        }
 });
  • When FCM Token changes, update user properties:
@Override
public void onNewToken(@NonNull String token) {
    JSONObject properties = new JSONObject();
    properties.put("fcm_token",token);
    instance.user_set(properties);
}

# 1.2. Acquire push click events

When tapping the notification, the user can track the push click events and get the push parameters in onCreate or onNewIntent.

public class PushOpenClickActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handlePushOpen();
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handlePushOpen();
    }

    private void handlePushOpen() {
        try {
            Intent intent = getIntent();
            if (intent == null) {
                return;
            }
            Bundle bundle = intent.getExtras();
            if (bundle == null) {
                return;
            }
            String teExtras = bundle.getString("te_extras");
            TAPushUtil.trackAppOpenNotification(teExtras);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

# 1.3. Handle push messages

public class PushOpenClickActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handlePushOpen();
    }

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

    private void handlePushOpen() {
        try {
            Intent intent = getIntent();
            if (intent == null) {
                return;
            }
            Bundle bundle = intent.getExtras();
            if (bundle == null) {
                return;
            }
            String teExtras = bundle.getString("te_extras");
            TAPushUtil.handleThinkingPushAction(teExtras);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

# JPush

# 2.1 Report "Push ID"

  • Track the Registration ID of JPush after calling TA login or switching accounts.
//call after logging in or switching accounts
instance.login("user_id");

JSONObject properties = new JSONObject();
properties.put("jiguang_id",JPushInterface.getRegistrationID(context));
//instance is TA instance
instance.user_set(properties);
  • Track the Registration ID of URORA in the onRegister interface provided by URORA.
public class PushMessageReceiver extends JPushMessageReceiver {
    @Override
    public void onRegister(Context context, String registrationId) {
        JSONObject properties = new JSONObject();
        properties.put("jiguang_id",registrationId);
        //instance is TA instance
        instance.user_set(properties);
    }
}

# 2.2. Acquire push click events

Non-vendor channels can send push click events in the onNotifyMessageOpened interface.

public class PushMessageReceiver extends JPushMessageReceiver {
    
    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {
        // send notification click events in notification click callback
        String te_extras = TAPushUtil.getPushExtras(message.notificationExtra);
        TAPushUtil.trackAppOpenNotification(te_extras);
    }
    
}

If a vendor channel is used, you need to get the intent in the onCreate and onNewIntent of the vendor channel Activity, parse out the corresponding parameters, and then send the push click event.

public class PushOpenClickActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handlePushOpen();
    }

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

    private void handlePushOpen() {
        try {
            Intent intent = getIntent();
            if (intent == null) {
                return;
            }
            String pushData = null;
            // message data from Huawei channel
            if (getIntent().getData() != null) {
                pushData = getIntent().getData().toString();
            }
            // message data from Xiaomi, vivo, OPPO and FCM channels (Meizu will call back onNotifyMessageOpened)
            if (TextUtils.isEmpty(pushData) && getIntent().getExtras() != null) {
                pushData = getIntent().getExtras().getString("JMessageExtra");
            }
            if (TextUtils.isEmpty(pushData)) {
                return;
            }
            JSONObject jsonObject = new JSONObject(pushData);
            // additional field for push messages
            String extras = jsonObject.optString("n_extras");
            String te_extras = TAPushUtil.getPushExtras(extras);
            TAPushUtil.trackAppOpenNotification(te_extras);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

# 2.3. Handle push messages

Non-vendor channels can handle push messages in the onNotifyMessageOpened.

public class PushMessageReceiver extends JPushMessageReceiver {
    
    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {
        //send notification click events in notification click callback
        String te_extras = TAPushUtil.getPushExtras(message.notificationExtra);
        TAPushUtil.handleThinkingPushAction(te_extras);
    }
    
}

If a vendor channel is used, you need to get the intent in the onCreate and onNewIntent of the vendor channel Activity, parse out the corresponding parameters, and then handle the push messages.

public class PushOpenClickActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handlePushOpen();
    }

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

    private void handlePushOpen() {
        try {
            Intent intent = getIntent();
            if (intent == null) {
                return;
            }
            String pushData = null;
            // message data from Huawei channel
            if (getIntent().getData() != null) {
                pushData = getIntent().getData().toString();
            }
            // message data from Xiaomi, vivo, OPPO and FCM channels (Meizu will call back onNotifyMessageOpened)
            if (TextUtils.isEmpty(pushData) && getIntent().getExtras() != null) {
                pushData = getIntent().getExtras().getString("JMessageExtra");
            }
            if (TextUtils.isEmpty(pushData)) {
                return;
            }
            JSONObject jsonObject = new JSONObject(pushData);
            // additional field for push messages
            String extras = jsonObject.optString("n_extras");
            String te_extras = TAPushUtil.getPushExtras(extras);
            TAPushUtil.handleThinkingPushAction(te_extras);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

#

# Appendix

TAPushUtil class

public class TAPushUtil {

    public static void trackAppOpenNotification(String extras) {
        try {
            if (TextUtils.isEmpty(extras)) {
                return;
            }
            JSONObject jsonObject = new JSONObject(extras);
            JSONObject properties = new JSONObject();
            properties.put("ops_receipt_properties", jsonObject.optJSONObject("ops_receipt_properties"));
            ThinkingAnalyticsSDK instance = ThinkingAnalyticsSDK.sharedInstance(null, "appId", "serverUrl");
            instance.track("ops_push_click", properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getPushExtras(Object notificationExtras) {
        String teExtras = "";
        try {
            if (notificationExtras != null) {
                if (notificationExtras instanceof String) {
                    teExtras = new JSONObject((String) notificationExtras).optString("te_extras");
                } else if (notificationExtras instanceof Map) {
                    teExtras = new JSONObject((Map) notificationExtras).optString("te_extras");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return teExtras;
    }

    public static void handleThinkingPushAction(String extras) {
        try {
            if (TextUtils.isEmpty(extras)) {
                return;
            }
            JSONObject jsonObject = new JSONObject(extras);
            String type = jsonObject.optString("ops_loading_type");
            if ("OPEN_APP".equals(type)) {
                // TODO handle open App message, --> please start App
            } else if ("OPEN_URL".equals(type)) {
                String url = jsonObject.optString("ops_url");
                if (!TextUtils.isEmpty(url)) {
                    // TODO handle open URL message, --> please handle URL
                }
            } else if ("CUSTOMIZED".equals(type)) {
                String custom = jsonObject.optString("ops_customized");
                if (!TextUtils.isEmpty(custom)) {
                    // TODO handle custom message, --> please handle custom message
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}