22
33import android .app .Activity ;
44import android .content .Context ;
5+ import android .content .Intent ;
56
67import androidx .annotation .Nullable ;
78
89import com .facebook .react .bridge .*;
910import com .zoyi .channel .plugin .android .ChannelIO ;
11+ import com .zoyi .channel .plugin .android .global .PrefSupervisor ;
1012import com .zoyi .channel .plugin .android .open .callback .BootCallback ;
1113import com .zoyi .channel .plugin .android .open .callback .UserUpdateCallback ;
1214import com .zoyi .channel .plugin .android .open .enumerate .BootStatus ;
@@ -22,6 +24,8 @@ public class RNChannelIO extends ReactContextBaseJavaModule implements ChannelPl
2224
2325 private ReactContext reactContext ;
2426
27+ private boolean hasPushNotificationClickSubscriber = false ;
28+
2529 public RNChannelIO (ReactApplicationContext reactContext ) {
2630 super (reactContext );
2731 this .reactContext = reactContext ;
@@ -46,6 +50,7 @@ public Map<String, Object> getConstants() {
4650 eventMap .put (Const .KEY_ON_FOLLOW_UP_CHANGED , Const .EVENT_ON_FOLLOW_UP_CHANGED );
4751 eventMap .put (Const .KEY_ON_URL_CLICKED , Const .EVENT_ON_URL_CLICKED );
4852 eventMap .put (Const .KEY_ON_PRE_URL_CLICKED , Const .EVENT_ON_PRE_URL_CLICKED );
53+ eventMap .put (Const .KEY_ON_PUSH_NOTIFICATION_CLICKED , Const .EVENT_ON_PUSH_NOTIFICATION_CLICKED );
4954
5055 constants .put (Const .KEY_EVENT , eventMap );
5156
@@ -240,9 +245,29 @@ public boolean onUrlClicked(String url) {
240245 return true ;
241246 }
242247
248+ @ ReactMethod
249+ public void notifyPushNotificationClickSubscriberExistence (boolean hasPushNotificationClickSubscriber ) {
250+ this .hasPushNotificationClickSubscriber = hasPushNotificationClickSubscriber ;
251+ }
252+
243253 @ Override
244- public boolean onPushNotificationClicked (String chatId ) {
245- return false ;
254+ public boolean onPushNotificationClicked (final String chatId ) {
255+ if (!hasPushNotificationClickSubscriber ) { return false ; }
256+
257+ // `PrefSupervisor` is an internal class that does not provide compatibility promise between SDK versions
258+ // -- avoid using it in third party libraries whenever possible as it may break at any time
259+ final String userId = PrefSupervisor .getLatestPushUserId (getCurrentActivity ());
260+ if (userId == null ) { return false ; }
261+
262+ Utils .sendEvent (
263+ reactContext ,
264+ Const .EVENT_ON_PUSH_NOTIFICATION_CLICKED ,
265+ ParseUtils .toWritableMap (new HashMap <String , Object >() {{
266+ put (Const .KEY_USER_ID , userId );
267+ put (Const .KEY_CHAT_ID , chatId );
268+ }})
269+ );
270+ return true ; // defer push notification click handling -- the JavaScript code will call `performDefaultPushNotificationClickAction` if needed.
246271 }
247272
248273 @ Override
@@ -259,6 +284,37 @@ public void handleUrlClicked(@Nullable String url) {
259284 }
260285 }
261286
287+ @ ReactMethod
288+ public void performDefaultPushNotificationClickAction (String userId , String chatId ) {
289+ if (reactContext == null ) { return ; }
290+ final Context context = reactContext .getApplicationContext ();
291+
292+ final Intent intent = reconstructHostAppIntent (userId , chatId );
293+ if (intent == null ) { return ; }
294+
295+ context .startActivity (intent );
296+ }
297+
298+ private Intent reconstructHostAppIntent (String userId , String chatId ) {
299+ // Channel Android SDK does not provide listeners a host app intent that the SDK prepared to
300+ // launch when onPushNotificationClicked() returns false. So "reconstruct" a new one to
301+ // emulate the default handling behavior of onPushNotificationClicked().
302+
303+ if (reactContext == null ) { return null ; }
304+ Context context = reactContext .getApplicationContext ();
305+
306+ Intent intent = context .getPackageManager ().getLaunchIntentForPackage (context .getPackageName ());
307+ if (intent == null ) { return null ; }
308+
309+ intent .addCategory (Intent .CATEGORY_LAUNCHER );
310+ // EXTRA_CHANNEL_ID is deliberately omitted because it is not used.
311+ intent .putExtra (Const .EXTRA_PERSON_TYPE , Const .USER );
312+ intent .putExtra (Const .EXTRA_PERSON_ID , userId );
313+ intent .putExtra (Const .EXTRA_CHAT_ID , chatId );
314+
315+ return intent ;
316+ }
317+
262318 @ ReactMethod
263319 public void setPage (@ Nullable String page ) {
264320 ChannelIO .setPage (page );
0 commit comments