2
2
3
3
import android .app .Activity ;
4
4
import android .content .Context ;
5
+ import android .content .Intent ;
5
6
6
7
import androidx .annotation .Nullable ;
7
8
8
9
import com .facebook .react .bridge .*;
9
10
import com .zoyi .channel .plugin .android .ChannelIO ;
11
+ import com .zoyi .channel .plugin .android .global .PrefSupervisor ;
10
12
import com .zoyi .channel .plugin .android .open .callback .BootCallback ;
11
13
import com .zoyi .channel .plugin .android .open .callback .UserUpdateCallback ;
12
14
import com .zoyi .channel .plugin .android .open .enumerate .BootStatus ;
@@ -22,6 +24,8 @@ public class RNChannelIO extends ReactContextBaseJavaModule implements ChannelPl
22
24
23
25
private ReactContext reactContext ;
24
26
27
+ private boolean hasPushNotificationClickSubscriber = false ;
28
+
25
29
public RNChannelIO (ReactApplicationContext reactContext ) {
26
30
super (reactContext );
27
31
this .reactContext = reactContext ;
@@ -46,6 +50,7 @@ public Map<String, Object> getConstants() {
46
50
eventMap .put (Const .KEY_ON_FOLLOW_UP_CHANGED , Const .EVENT_ON_FOLLOW_UP_CHANGED );
47
51
eventMap .put (Const .KEY_ON_URL_CLICKED , Const .EVENT_ON_URL_CLICKED );
48
52
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 );
49
54
50
55
constants .put (Const .KEY_EVENT , eventMap );
51
56
@@ -240,9 +245,29 @@ public boolean onUrlClicked(String url) {
240
245
return true ;
241
246
}
242
247
248
+ @ ReactMethod
249
+ public void notifyPushNotificationClickSubscriberExistence (boolean hasPushNotificationClickSubscriber ) {
250
+ this .hasPushNotificationClickSubscriber = hasPushNotificationClickSubscriber ;
251
+ }
252
+
243
253
@ 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.
246
271
}
247
272
248
273
@ Override
@@ -259,6 +284,37 @@ public void handleUrlClicked(@Nullable String url) {
259
284
}
260
285
}
261
286
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
+
262
318
@ ReactMethod
263
319
public void setPage (@ Nullable String page ) {
264
320
ChannelIO .setPage (page );
0 commit comments