1

我正在尝试使用 gcm 在 sinch 中启动呼叫,我发送推送通知并在另一端收到它,但是当我尝试启动呼叫时,我的服务中出现空指针异常mCall.addCallListener( );

这是我收到通知并启动 sinch 服务的 gcm 意图服务的一部分:

{
            String callId = intent.getExtras().getString("callId");
            Intent intent3 = new Intent(this, SinchClientService.class);
            intent3.setAction(SinchClientService.ACTION_START_CALL);
            intent3.putExtra(SinchClientService.INTENT_EXTRA_CALLID, callId);
            startService(intent3);
        }

这就是我在 sinch 服务中所做的:

else if(intent.getAction().equals(ACTION_START_CALL))
            {
                String callId = intent.getStringExtra(INTENT_EXTRA_CALLID);
                if(callId != null)
                {
                    startCall(callId);
                }
            }

public void startCall(String callId) {
        if (mCallClient != null) {
            Call call = mCallClient.getCall(callId);
            CurrentCall.currentCall = call;

            Intent intent = new Intent(this, CallService.class);
            startService(intent);
        }
    }

这就是我开始sinch客户端的方式:

private void startSinchClient(String id, String userName) {
        mSinchClient = Sinch.getSinchClientBuilder().context(this).userId(id).
                applicationKey(APP_KEY).applicationSecret(APP_SECRET).environmentHost(ENVIRONMENT).build();

        mSinchClient.addSinchClientListener(this);

        mSinchClient.setSupportCalling(true);
        mSinchClient.setSupportMessaging(true);
        mSinchClient.setSupportPushNotifications(true);

        mSinchClient.setSupportActiveConnectionInBackground(false);
        //mSinchClient.startListeningOnActiveConnection();

        mMessageClient = mSinchClient.getMessageClient();
        mMessageClient.addMessageClientListener(this);

        mCallClient = mSinchClient.getCallClient();
        mCallClient.addCallClientListener(this);

        mSinchClient.checkManifest();
        mSinchClient.start();
    }

这是我的 onIcommingCall 方法:

@Override
    public void onIncomingCall(CallClient client, Call call) {
        Log.d(TAG, "Incoming call");

        CurrentCall.currentCall = call;


        Intent intent = new Intent(this, CallService.class);
        startService(intent);
    }

这是我的呼叫服务的一部分,我得到空指针异常:

if(CurrentCall.currentCall == null)
            stopSelf();

        mCall = CurrentCall.currentCall;
        mCall.addCallListener(this);

注意:我的呼叫服务实现呼叫监听器

我从来没有接到电话,有人可以帮助我吗?

编辑这是我初始化呼叫的方式:

在 sinch 服务中:

public void callFriend(String id) {
        if (mCallClient != null) {
            Call call = mCallClient.callUser(id);
            CurrentCall.currentCall = call;

            Intent intent = new Intent(this, CallService.class);
            startService(intent);
        }
    }

在呼叫服务中:

@Override
    public void onShouldSendPushNotification(Call call, List<PushPair> pushPairs) {
        Log.d(LOG_TAG, "Should send push notification");
        Log.d("payload", pushPairs.get(0).getPushPayload());

        asyncTask = new sendPushNotifications(this, call.getRemoteUserId(), pushPairs, call.getCallId());
        asyncTask.execute();
    }

    class sendPushNotifications extends AutoAsyncTask {

        List<PushPair> pushPairs;

        String message;

        String senderId;
        String message_id;
        String time_stamp;
        String callId;

        public sendPushNotifications(Context context, String senderId, List<PushPair> pushPairs, String callId) {
            super(context, false);

            this.pushPairs = pushPairs;
            this.senderId = senderId;
            this.callId = callId;
        }

        @Override
        protected Integer doInBackground(Void... params) {
            boolean connectedToInternet = connManager.getNetworkInfo( ConnectivityManager.TYPE_WIFI).isConnected()
                    || connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
            if (connectedToInternet) 
            {   
                String regId;
                //JSONArray arrayList = new JSONArray();

                for(PushPair p: pushPairs)
                {
                    regId = new String(p.getPushData());
                    //arrayList.put(regId);

                    UserFunctions.sendCallPushNotifications(senderId, regId, "call", callId);
                }

                asyncTask = null;
                return 0;
            } 
            else 
            {
                asyncTask = null;
                return 1;
            }
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);

            if (result == 0) 
            {
                Log.d("call push sent", "sent");
            } 
            else if (result == 1) 
            {
                Toast.makeText(getApplicationContext(),
                        "No Network Connection !!", Toast.LENGTH_SHORT).show();
            } 
            else 
            {
                Toast.makeText(
                        getApplicationContext(),
                        "Error occured,Please Try Again Later !!",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

}

最新编辑所以我按照你说的做了,我只在消息活动中接到电话,我的新代码是:

在呼叫服务中:

@Override
    public void onShouldSendPushNotification(Call call, List<PushPair> pushPairs) {
        Log.d(LOG_TAG, "Should send push notification");
        Log.d("payload", pushPairs.get(0).getPushPayload());

        asyncTask = new sendPushNotifications(this, call.getRemoteUserId(), pushPairs, call.getCallId());
        asyncTask.execute();
    }

    class sendPushNotifications extends AutoAsyncTask {

        List<PushPair> pushPairs;

        String message;

        String senderId;
        String message_id;
        String time_stamp;
        String callId;

        public sendPushNotifications(Context context, String senderId, List<PushPair> pushPairs, String callId) {
            super(context, false);

            this.pushPairs = pushPairs;
            this.senderId = senderId;
            this.callId = callId;
        }

        @Override
        protected Integer doInBackground(Void... params) {
            boolean connectedToInternet = connManager.getNetworkInfo( ConnectivityManager.TYPE_WIFI).isConnected()
                    || connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
            if (connectedToInternet) 
            {   
                String regId;
                String payload;
                //JSONArray arrayList = new JSONArray();

                for(PushPair p: pushPairs)
                {
                    regId = new String(p.getPushData());
                    payload = new String(p.getPushPayload());
                    //arrayList.put(regId);

                    UserFunctions.sendCallPushNotifications(senderId, regId, "call", callId, payload);
                }

                asyncTask = null;
                return 0;
            } 
            else 
            {
                asyncTask = null;
                return 1;
            }
        }

        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);

            if (result == 0) 
            {
                Log.d("call push sent", "sent");
            } 
            else if (result == 1) 
            {
                Toast.makeText(getApplicationContext(),
                        "No Network Connection !!", Toast.LENGTH_SHORT).show();
            } 
            else 
            {
                Toast.makeText(
                        getApplicationContext(),
                        "Error occured,Please Try Again Later !!",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

gcm 意图服务:

else
        {
            String callId = intent.getExtras().getString("callId");
            String payload = intent.getExtras().getString("payload");
            Intent intent3 = new Intent(this, SinchClientService.class);
            intent3.setAction(SinchClientService.ACTION_START_CALL);
            intent3.putExtra(SinchClientService.INTENT_EXTRA_CALLID, callId);
            intent3.putExtra(SinchClientService.INTENT_EXTRA_PAYLOAD, payload);
            startService(intent3);
        }

新奇服务:

else if(intent.getAction().equals(ACTION_START_CALL))
            {
                String callId = intent.getStringExtra(INTENT_EXTRA_CALLID);
                if(callId != null)
                {
                    startCall(callId);
                }
            }

public void startCall(String callId) {
        /*if (mCallClient != null) {
            Call call = mCallClient.getCall(callId);
            CurrentCall.currentCall = call;

            Intent intent = new Intent(this, CallService.class);
            startService(intent);
        }*/
        if(INTENT_EXTRA_PAYLOAD != null)
        {
            if(mSinchClient.isStarted())
            mSinchClient.relayRemotePushNotificationPayload(INTENT_EXTRA_PAYLOAD);
            else
            {
                DatabaseHandler handler = new DatabaseHandler(this);
                UserInfo info = handler.getUserDetails();
                startSinchClient(String.valueOf(info.uid), info.name);

                String gcm_regId = ChatDatabaseHandler.getInstance(this).getGcmRegId();

                mSinchClient.registerPushNotificationData(gcm_regId.getBytes());
            }
        }
    }

@Override
    public void onIncomingCall(CallClient client, Call call) {
        Log.d(TAG, "Incoming call");
        /*if(INTENT_EXTRA_CALLID != null)
        {
        CurrentCall.currentCall = mCallClient.getCall(INTENT_EXTRA_CALLID);
        INTENT_EXTRA_CALLID = null;
        }*/
        CurrentCall.currentCall = call;

        Intent intent = new Intent(this, CallService.class);
        startService(intent);
    }

这也是我在绑定和取消绑定服务时所做的:

@Override
    public boolean onUnbind(Intent intent) {
        mSinchClient.stopListeningOnActiveConnection();
        return super.onUnbind(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
        mSinchClient.startListeningOnActiveConnection();
        return mServiceInterface;
    }
4

1 回答 1

3

您似乎缺少在推送中实际发送特定于 Sinch 的有效负载的代码,您应该使用PushPair.getPushPayload()它在onShouldSendPushNotification回调中检索它并确保将其包含在推送消息中。
添加后,您需要在接收方检索有效负载并传递给relayRemotePushNotificationPayload(String payload). 这将为您提供onIncomingCall接收方的回调。

于 2015-03-18T12:37:16.063 回答