I am trying to make a custom incoming call notification like WhatsApp for my app if I don’t use this custom layout then my notification is working. I am trying this for the first time any help will be appreciated.
This is my FireBaseMessagingService Class
public class MyFireBaseMessagingService extends FirebaseMessagingService { private String CHANNEL_ID = "channel-02"; private String CHANNEL_NAME = "Channel Ring"; long[] pattern = {500, 500, 500, 500, 500, 500, 500, 500, 500}; @Override public void onNewToken(@NonNull String s) { super.onNewToken(s); Log.e("NewToken", s); getSharedPreferences("_", MODE_PRIVATE).edit().putString("fb", s).apply(); } @RequiresApi(api = Build.VERSION_CODES.P) @Override public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); Log.e("Notification", remoteMessage.getFrom()); showSimpleNotification(getApplicationContext(), remoteMessage, remoteMessage.getData().get("title"), remoteMessage.getData().get("body")); } public static String getToken(Context context) { return context.getSharedPreferences("_", MODE_PRIVATE).getString("fb", "empty"); } @SuppressLint("RemoteViewLayout") @RequiresApi(api = Build.VERSION_CODES.P) public void showFullScreenIntent(RemoteMessage remoteMessage, String title, String body) { Intent receiveCallIntent = new Intent(getApplicationContext(), FCMReceiver.class); receiveCallIntent.putExtra("appointment_id", remoteMessage.getData().get("appointment_id")); receiveCallIntent.putExtra("message", title); receiveCallIntent.setAction("RECEIVE_CALL"); Intent cancelCallIntent = new Intent(getApplicationContext(), FCMReceiver.class); receiveCallIntent.putExtra("appointment_id", remoteMessage.getData().get("appointment_id")); receiveCallIntent.putExtra("message", "Call Rejected"); receiveCallIntent.setAction("CANCEL_CALL"); PendingIntent receiveCallPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1200, receiveCallIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent cancelCallPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1201, cancelCallIntent, PendingIntent.FLAG_CANCEL_CURRENT); Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_call_notification); remoteViews.setImageViewResource(R.id.caller_image, R.drawable.icon); remoteViews.setTextViewText(R.id.caller_name, body); remoteViews.setTextViewText(R.id.call_type, title); remoteViews.setOnClickPendingIntent(R.id.btnAccept, receiveCallPendingIntent); remoteViews.setOnClickPendingIntent(R.id.btnDecline, cancelCallPendingIntent); @SuppressLint("ResourceAsColor") NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID) .setSmallIcon(R.drawable.icon) .setStyle(new NotificationCompat.DecoratedCustomViewStyle()) .setCustomContentView(remoteViews) .setCustomHeadsUpContentView(remoteViews) .setPriority(NotificationCompat.PRIORITY_MAX) .setAutoCancel(true) .setTimeoutAfter(30000) .setOngoing(true) .setVibrate(pattern) .setSound(ringtone) .setFullScreenIntent(receiveCallPendingIntent, true); NotificationChannel channel = createChannel(); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification incomingCallNotification = notificationBuilder.build(); mNotificationManager.createNotificationChannel(channel); mNotificationManager.notify(120, incomingCallNotification); }
This is my channel
public NotificationChannel createChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, importance); channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); channel.setDescription("Call Notifications"); channel.setSound(Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.ringtone), new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) .setLegacyStreamType(AudioManager.STREAM_RING) .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION).build()); channel.shouldVibrate(); channel.enableVibration(true); channel.setVibrationPattern(pattern); Objects.requireNonNull(getApplicationContext().getSystemService(NotificationManager.class)).createNotificationChannel(channel); return channel; } return null; }
This is my FCMReceiver Class
public class FCMReceiver extends BroadcastReceiver { private Context mContext; private String mTitle; private String mContent; String action = ""; @Override public void onReceive(Context context, Intent intent) { if (context != null) { mContext = context; } Log.e("Receiver", "Receiver a notification"); mTitle = intent.getStringExtra("message"); if (mTitle != null && !mTitle.isEmpty()) { Log.e("action", "message : " + mTitle); performActionClicks(context, mTitle, intent); Intent iclose = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); context.sendBroadcast(iclose); context.stopService(new Intent(context, MyFireBaseMessagingService.class)); } if (intent.getStringExtra("gcm.notification.title") != null) { if (intent.getStringExtra("gcm.notification title").equalsIgnoreCase("New Appointment")) { ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo(); ActivityManager.getMyMemoryState(myProcess); boolean isInBackground = myProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND; if (isInBackground) { Intent launchIntent = new Intent(context, HomeActivity.class); launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); launchIntent.putExtra("appointment_id", intent.getStringExtra("appointment_id")); } } } if (intent.getStringExtra("notification") != null) { } } private void performActionClicks(Context context, @NonNull String mTitle, Intent intent) { if (!mTitle.isEmpty()) { if (mTitle.equalsIgnoreCase("Ringing")) { Intent intentCallReceive = new Intent(context, VideoChatViewActivity.class) .putExtra("appointment_id", intent.getStringExtra("appointment_id")); intentCallReceive.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); mContext.startActivity(intentCallReceive); NotificationManagerCompat.from(mContext).cancel(null, 120); } else if (mTitle.equalsIgnoreCase("Declined")) { } else if (mTitle.equalsIgnoreCase("Call Rejected")) { NotificationManagerCompat.from(mContext).cancel(null, 120); } } }
Advertisement
Answer
In you NotificationBuilder you need to set notification category to NotificationCompat.CATEGORY_CALL and style to NotificationCompat.BigTextStyle() like this
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID) .setSmallIcon(R.drawable.icon) .setStyle(new NotificationCompat.DecoratedCustomViewStyle()) .setCustomContentView(remoteViews) .setCustomHeadsUpContentView(remoteViews) .setPriority(NotificationCompat.PRIORITY_MAX) .setAutoCancel(true) .setTimeoutAfter(30000) .setStyle(NotificationCompat.BigTextStyle()) .setCategory(NotificationCompat.CATEGORY_CALL) .setOngoing(true) .setVibrate(pattern) .setSound(ringtone) .setFullScreenIntent(receiveCallPendingIntent, true);