Android to android:
- when app is in foreground, notification service send "Incoming Call" to starting call (work good)
- when a call started and app came to foreground, notification service send "Incoming Call" to starting call (work good)
iOS to android:
- when app is in foreground, notification service send "Incoming Call" to starting call (work good)
- when a call started and app came to foreground, notification service don't send "Incoming Call" to starting call (My problem!)
MessagingService:
public class MessagingService extends FirebaseMessagingService {
private int notificationNumber = 1;
private LocalBroadcastManager broadcaster;
private int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
private DatabaseReference mDatabase;
private SharedPref pref;
public static boolean firsttimeFun = true;
@Override
public void onCreate() {
broadcaster = LocalBroadcastManager.getInstance(this);
pref = new SharedPref(this);
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// String bundleString = remoteMessage.getNotification().getBody();
Map bundle = remoteMessage.getData();
String callId = bundle.get("callID").toString();
String groupname = bundle.get("group").toString();
try {
bundle.get("hangup").toString();
Intent intent = new Intent("MyData");
intent.putExtra("callID", callId);
broadcaster.sendBroadcast(intent);
return;
} catch (Exception e) {
e.printStackTrace();
}
//IF app is in background use return for ignore notifications
if (!AppLifecycleListener.isActivityVisible()) {
return;
}
Log.i("MessageFrom", "From: " + callId);
if (firsttimeFun) {
firsttimeFun = false;
startActivity(new Intent(this, CallActivity.class).putExtra("callerId", callId).putExtra("name", groupname).putExtra("isOutGoing", false).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
if (AppLifecycleListener.isActivityVisible())
sendNotification(callId, groupname);
// sendNotification(bundle.get("message").toString());
}
@Override
public void onNewToken(String s) {
/* HashMap map=new HashMap();
map.put("fcmToken",s);
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("Users").child(pref.getUserId()).updateChildren(map);*/
}
@RequiresApi(api = Build.VERSION_CODES.N)
private void sendNotification(String callId, String groupname) {
PendingIntent contentIntent;
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, CallActivity.class).putExtra("callerId", callId).putExtra("name", groupname).putExtra("isOutGoing", false), 0);
int notifyID = 1;
String CHANNEL_ID = "CallAppChanel";// The id of the channel.
CharSequence name = getString(R.string.channel_name);// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(CHANNEL_ID, "Channel 1", importance);
}
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Incoming Call")
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setContentIntent(contentIntent)
.setColor(getResources().getColor(R.color.purple))
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setChannelId(CHANNEL_ID)
.setPriority(importance)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.build();
int notificationId = new Random().nextInt(100) + 1;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.createNotificationChannel(mChannel);
mNotificationManager.notify(notificationId, notification);
} else {
mNotificationManager.notify(notificationId, notification);
}
}
}
PushNotificationClass:
public class PushNotificationClass {
private String FCM_API = "https://fcm.googleapis.com/fcm/send";
private String serverkey = "key=AAAAr...
private String contentType = "application/json";
private Context context;
public PushNotificationClass(Context context) {
this.context = context;
}
public void jsonObjectConversion(String message, String to,String groupid,String groupname,String source) {
JSONObject notification = new JSONObject();
JSONObject notifcationBody = new JSONObject();
try {
if(source.equals("android")) {
notification.put("to", to);
notifcationBody.put("title", "Incoming Call");
notifcationBody.put("message", message);
notifcationBody.put("callID", groupid);
notifcationBody.put("group", groupname);
//Enter your notification message
notifcationBody.put("sound", "default");
notifcationBody.put("mutable_content", true);
notification.put("data", notifcationBody);
notification.put("priority", 10);
}
else {
notifcationBody.put("title", "Incoming Call");
notifcationBody.put("message", message);
notifcationBody.put("callID", groupid);
notifcationBody.put("group", groupname);
//Enter your notification message
notification.put("to", to);
notification.put("sound", "default");
notification.put("mutable_content", true);
notification.put("notification", notifcationBody);
notification.put("data", notifcationBody);
}
Log.e("TAG", "try");
} catch (JSONException e) {
Log.e("TAG", "onCreate: " + e.getMessage());
}
sendNotification(notification);
}
public void jsonObjectForHangUpConversion(String message, String to,String groupid,String name) {
JSONObject notification = new JSONObject();
JSONObject notifcationBody = new JSONObject();
try {
notifcationBody.put("title", "Audio Call");
notifcationBody.put("message", message);
notifcationBody.put("callID", groupid);
notifcationBody.put("group", "English");
notifcationBody.put("hangup", "1");
//Enter your notification message
notification.put("to", to);
notification.put("sound", "default");
notification.put("mutable_content", true);
notification.put("notification", notifcationBody);
notification.put("data", notifcationBody);
Log.e("TAG", "try");
} catch (JSONException e) {
Log.e("TAG", "onCreate: " + e.getMessage());
}
sendNotification(notification);
}
public void sendNotification(JSONObject notification)
{
Log.i("chkres", notification.toString());
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, FCM_API, notification, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("log",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
NetworkResponse response = error.networkResponse;
if (error instanceof ServerError && response != null) {
try {
String res = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, "utf-8"));
// Now you can use any deserializer to make sense of data
JSONObject obj = new JSONObject(res);
} catch (UnsupportedEncodingException | JSONException e1) {
// Couldn't properly decode data to string
Toast.makeText(context, e1.getMessage(), Toast.LENGTH_SHORT).show();
} // returned data is not JSONObject?
}
}
}) {
@Override
public Map<String, String> getHeaders() {
HashMap<String,String> params = new HashMap<>();
params.put("Authorization",serverkey);
params.put("Content-Type",contentType);
return params;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(
9000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Log.d("getContentType", request.getHeaders().toString());
RequestQueue queue = Volley.newRequestQueue(context);
queue.add(request);
}
}