Search in sources :

Example 1 with CallInvite

use of com.twilio.voice.CallInvite in project react-native-twilio-programmable-voice by hoxfon.

the class VoiceFirebaseMessagingService method onMessageReceived.

/**
 * Called when message is received.
 *
 * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
 */
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "Bundle data: " + remoteMessage.getData());
    }
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Map<String, String> data = remoteMessage.getData();
        // If notification ID is not provided by the user for push notification, generate one at random
        Random randomNumberGenerator = new Random(System.currentTimeMillis());
        final int notificationId = randomNumberGenerator.nextInt();
        Voice.handleMessage(this, data, new MessageListener() {

            @Override
            public void onCallInvite(final CallInvite callInvite) {
                // We need to run this on the main thread, as the React code assumes that is true.
                // Namely, DevServerHelper constructs a Handler() without a Looper, which triggers:
                // "Can't create handler inside thread that has not called Looper.prepare()"
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {

                    public void run() {
                        // Construct and load our normal React JS code bundle
                        ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
                        ReactContext context = mReactInstanceManager.getCurrentReactContext();
                        // If it's constructed, send a notification
                        if (context != null) {
                            int appImportance = callNotificationManager.getApplicationImportance((ReactApplicationContext) context);
                            if (BuildConfig.DEBUG) {
                                Log.d(TAG, "CONTEXT present appImportance = " + appImportance);
                            }
                            Intent launchIntent = callNotificationManager.getLaunchIntent((ReactApplicationContext) context, notificationId, callInvite, false, appImportance);
                            // app is not in foreground
                            if (appImportance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                                context.startActivity(launchIntent);
                            }
                            VoiceFirebaseMessagingService.this.handleIncomingCall((ReactApplicationContext) context, notificationId, callInvite, launchIntent);
                        } else {
                            // Otherwise wait for construction, then handle the incoming call
                            mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {

                                public void onReactContextInitialized(ReactContext context) {
                                    int appImportance = callNotificationManager.getApplicationImportance((ReactApplicationContext) context);
                                    if (BuildConfig.DEBUG) {
                                        Log.d(TAG, "CONTEXT not present appImportance = " + appImportance);
                                    }
                                    Intent launchIntent = callNotificationManager.getLaunchIntent((ReactApplicationContext) context, notificationId, callInvite, true, appImportance);
                                    context.startActivity(launchIntent);
                                    VoiceFirebaseMessagingService.this.handleIncomingCall((ReactApplicationContext) context, notificationId, callInvite, launchIntent);
                                }
                            });
                            if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
                                // Construct it in the background
                                mReactInstanceManager.createReactContextInBackground();
                            }
                        }
                    }
                });
            }

            @Override
            public void onError(MessageException messageException) {
                Log.e(TAG, "Error handling FCM message" + messageException.toString());
            }
        });
    }
    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
    }
}
Also used : ReactInstanceManager(com.facebook.react.ReactInstanceManager) MessageListener(com.twilio.voice.MessageListener) Handler(android.os.Handler) Intent(android.content.Intent) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) Random(java.util.Random) ReactContext(com.facebook.react.bridge.ReactContext) MessageException(com.twilio.voice.MessageException) CallInvite(com.twilio.voice.CallInvite)

Aggregations

Intent (android.content.Intent)1 Handler (android.os.Handler)1 ReactInstanceManager (com.facebook.react.ReactInstanceManager)1 ReactApplicationContext (com.facebook.react.bridge.ReactApplicationContext)1 ReactContext (com.facebook.react.bridge.ReactContext)1 CallInvite (com.twilio.voice.CallInvite)1 MessageException (com.twilio.voice.MessageException)1 MessageListener (com.twilio.voice.MessageListener)1 Random (java.util.Random)1