Call notification in Android

we have some issues in getting call notifications in Android when the application is in the background. Most of the time notification is not coming.

we are using the following code

++++++++++++++++++++++++++++++++++++++++++++++++
public static void showCallNotifcation(Context context, Call call) {
try {
new Thread(new Runnable() {
@Override
public void run() {
int REQUEST_CODE = 12;
int m = (int) ((new Date().getTime()));
String GROUP_ID = “group_id”;
String receiverName="",callType,receiverAvatar="",receiverUid="";

            if (call.getReceiverType().equals(CometChatConstants.RECEIVER_TYPE_USER) && call.getSender().getUid().equals(CometChat.getLoggedInUser().getUid()))
            {
                receiverUid = ((User)call.getCallReceiver()).getUid();
                receiverName = ((User)call.getCallReceiver()).getName();
                receiverAvatar = ((User)call.getCallReceiver()).getAvatar();
            } else if(call.getReceiverType().equals(CometChatConstants.RECEIVER_TYPE_USER)) {
                receiverUid = call.getSender().getUid();
                receiverName = call.getSender().getName();
                receiverAvatar = call.getSender().getAvatar();
            } else {
                receiverUid = ((Group)call.getReceiver()).getGuid();
                receiverName = ((Group)call.getReceiver()).getName();
                receiverAvatar = ((Group)call.getReceiver()).getIcon();
            }

            if (call.getType().equals(CometChatConstants.CALL_TYPE_AUDIO)) {
                callType = context.getResources().getString(R.string.incoming_audio_call);
            }

            else {
                callType = context.getResources().getString(R.string.incoming_video_call);
            }

            Intent callIntent;
            callIntent = new Intent(context, IncomingCallActivity.class);
            callIntent.putExtra(StringContract.IntentStrings.NAME, receiverName);
            callIntent.putExtra(StringContract.IntentStrings.UID, receiverUid);
            callIntent.putExtra(StringContract.IntentStrings.SESSION_ID, call.getSessionId());
            callIntent.putExtra(StringContract.IntentStrings.AVATAR, receiverAvatar);
            callIntent.setAction(call.getType());
            callIntent.setType("incoming");

            NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"2")
                    .setSmallIcon(R.drawable.appsicons)
                    .setContentTitle(receiverName)
                    .setContentText(callType)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setChannelId("2")
                    .setColor(context.getResources().getColor(R.color.colorPrimary))
                    .setLargeIcon(getBitmapFromURL(receiverAvatar))
                    .setGroup(GROUP_ID)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

            builder.setGroup(GROUP_ID+"Call");
            builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
            builder.addAction(0, "Answers", PendingIntent.getBroadcast(context, REQUEST_CODE, callIntent, PendingIntent.FLAG_UPDATE_CURRENT));
            builder.addAction(0, "Decline", PendingIntent.getBroadcast(context, 1, callIntent, PendingIntent.FLAG_UPDATE_CURRENT));
            notificationManager.notify(05,builder.build());

        }
    }).start();
} catch (NullPointerException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

}

modification in comet chat calling method
+++++++++++++++++++++++++++++++++++++++++++++++++
public static void showCallNotifcation2(Context context ,String callType,String receiverName,String receiverAvatar, String receiverUid, String seesionid) {
try {
new Thread(new Runnable() {
@Override
public void run() {
int REQUEST_CODE = 12;
int m = (int) ((new Date().getTime()));
String GROUP_ID = “group_id”;

            Intent callIntent;
            callIntent = new Intent(context, IncomingCallActivity.class);
            callIntent.putExtra(StringContract.IntentStrings.NAME, receiverName);
            callIntent.putExtra(StringContract.IntentStrings.UID, receiverUid);
            callIntent.putExtra(StringContract.IntentStrings.SESSION_ID, seesionid);
            callIntent.putExtra(StringContract.IntentStrings.AVATAR, receiverAvatar);
            callIntent.setAction(callType);
            callIntent.setType("incoming");
            callIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, callIntent,
                PendingIntent.FLAG_ONE_SHOT);
            Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"2")
                    .setSmallIcon(R.drawable.appsicons)
                    .setContentTitle(receiverName)
                    .setContentText(callType)
                    .setPriority(Notification.PRIORITY_HIGH)
                    .setChannelId("2")
            .setDefaults(Notification.DEFAULT_VIBRATE)
                .setSound(defaultSoundUri)
                .setDefaults(Notification.DEFAULT_ALL)
                    .setCategory(NotificationCompat.CATEGORY_CALL)
                    .setAutoCancel(true)
                    .setColor(context.getResources().getColor(R.color.colorPrimary))
                    .setLargeIcon(getBitmapFromURL(receiverAvatar))
                    .setGroup(GROUP_ID)
                    .setContentIntent(pendingIntent);

                 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

            builder.setGroup(GROUP_ID+"Call");
            builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
            builder.setOngoing(true);
                builder.addAction(0, "Answers", PendingIntent.getBroadcast(context, REQUEST_CODE, getCallIntent("Answers",context,seesionid), PendingIntent.FLAG_UPDATE_CURRENT));
                builder.addAction(0, "Decline", PendingIntent.getBroadcast(context, 1, getCallIntent("Decline",context,seesionid), PendingIntent.FLAG_UPDATE_CURRENT));

            notificationManager.notify(05,builder.build());
        }
    }).start();
} catch (NullPointerException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

}

Broadcasting for performing calling methods
+++++++++++++++++++++++

package com.selfychat.helper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationManagerCompat;

import com.cometchat.pro.constants.CometChatConstants;
import com.cometchat.pro.core.Call;
import com.cometchat.pro.core.CometChat;
import com.cometchat.pro.exceptions.CometChatException;
import com.selfychat.ui.activity.CallActivity;
import com.selfychat.contracts.StringContract;

public class CallNotificationAction extends BroadcastReceiver {

String TAG = "CallNotificationAction";
@Override
public void onReceive(Context context, Intent intent) {
    String sessionID = intent.getStringExtra(StringContract.IntentStrings.SESSION_ID);
    Log.e(TAG, "onReceive: " + intent.getStringExtra(StringContract.IntentStrings.SESSION_ID));
    if (intent.getAction().equals("Answers"))
    {
        CometChat.acceptCall(sessionID, new CometChat.CallbackListener<Call>() {
            @Override
            public void onSuccess(Call call) {
                Intent acceptIntent = new Intent(context, CallActivity.class);
                acceptIntent.putExtra(StringContract.IntentStrings.SESSION_ID,call.getSessionId());
                acceptIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(acceptIntent);
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
                notificationManager.cancel(05);
            }

            @Override
            public void onError(CometChatException e) {
                Toast.makeText(context,"Error "+e.getMessage(),Toast.LENGTH_LONG).show();
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
                notificationManager.cancel(05);
            }
        });
    }
    else {
        CometChat.rejectCall(sessionID, CometChatConstants.CALL_STATUS_REJECTED, new CometChat.CallbackListener<Call>() {
            @Override
            public void onSuccess(Call call) {
                Log.e(TAG, "onSuccess: " + call.getCallStatus());
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
                notificationManager.cancel(05);
            }

            @Override
            public void onError(CometChatException e) {
                Log.e(TAG, "onError: " + e.getCode() + e.getDetails());
            }
        });
    }
}

}

Resources
++++++++++++

Hi,

Based on the code you shared, it seems like you are using Intent to launch activity “IncomingCallActivity” whenever “Answer” or “Decline” action is clicked. Since you are not using the BroadCast Receiver “CallNotificationAction” that you have mentioned in your code you might not receive notification in the background due to app restrictions.

We suggest you to please refer to our push notification sample app mentioned in below link.