I am using Firebase Notification
on my app I am getting notification properly but when I click on the notification my app is not opening
this is my class
public class MyFirebaseMessagingService extends FirebaseMessagingService {
Intent intent ;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String body = "";
String title = "";
String type = "";
String json_Data = "";
String referenceId = "";
Log.d("FirebaseMsgService", "Message received: " + remoteMessage);
try {
if (remoteMessage.getNotification() != null) {
body = Objects.requireNonNull(remoteMessage.getNotification()).getBody();
title = remoteMessage.getNotification().getTitle();
type = remoteMessage.getNotification().getClickAction();
/* body = remoteMessage.getNotification().getBody();
title = remoteMessage.getNotification().getTitle();*/
Log.d("FirebaseMsgService", "Notification received - Title: " + title + ", Body: " + body + ", Type: " + type);
}
referenceId = remoteMessage.getData().get("refrence_id");
// type = remoteMessage.getData().get("click_action"); // Get the click_action
Log.d("FirebaseMsgService", "Reference ID: " + referenceId);
}catch (Exception e){
type ="";
Log.d("FirebaseMsgService", "" + e.toString());
Log.d("FirebaseMsgService",e.toString() + "" +e.getMessage().toString());
}
sendNotification(body , title , type , referenceId);
}
private void sendNotification(String body , String title , String noti_type , String postId) {
Log.d("FirebaseMsgService", "Intent Data: noti_type=" + noti_type + ", postId=" + postId);
if(noti_type.equals("disscussion_board")){
intent = new Intent(getApplicationContext(), DetailDiscussionActivity.class);
Bundle bundle = new Bundle();
bundle.putString("borad_id" , postId);
intent.putExtra( "sss" ,bundle);
} else if(noti_type.equals("sell_vehicle")){
intent = new Intent(getApplicationContext(), CarListForSaleActivty.class);
Bundle bundle = new Bundle();
bundle.putString("sell_id" , postId);
bundle.putString("title" , title);
intent.putExtra( "sss" ,bundle);
} else if (noti_type.equals("portable_charger_booking")) {
intent = new Intent(getApplicationContext(), PortableChargerBookingHistoryDetailsActivity.class);
intent.putExtra("booking_id" , postId);
intent.putExtra("title" , title);
// intent.putExtra( "sss" ,bundle);
} else if (noti_type.equals("charging_service")) {
intent = new Intent(getApplicationContext(), ChargingServiceHistoryDetailActivity.class);
intent.putExtra("service_id" , postId);
intent.putExtra("title" , title);
// intent.putExtra( "sss" ,bundle);
}else if(noti_type.equals("charging_installation_service")){
intent = new Intent(getApplicationContext(), ChargerInstallationHistoryDetailActivity.class);
intent.putExtra("requestId" , postId);
intent.putExtra("title" , title);
}else {
intent = new Intent(getApplicationContext(), NotificationListActivity.class);
}
int requestCode = (int) System.currentTimeMillis();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), requestCode, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channelId");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("channelId", getString(R.string.app_name), NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
notificationBuilder.setChannelId("channelId");
}
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000, 1000})
.setContentIntent(pendingIntent);
Random rand = new Random();
int as = rand.nextInt();
// NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(as /* ID of notification */, notificationBuilder.build());
}
}
and nothing is printing on Logs
this is data coming from server
"message": {
"token": "dSfgsgsfdgsdfgdsfxxxxxxxxxxxxxxxxxxxxxxxxxx",
"notification": {
"title": "Completed!",
"body": "complete body ."
},
"data": {
"title": "Charging Completed!",
"body": "Charging complete, please lock your EV.",
"click_action": "portable_charger_booking",
"refrence_id": "PCB0305"
},
"apns": {
"payload": {
"aps": {
"sound": "default"
}
}
},
"android": {
"priority": "high",
"notification": {
"click_action": "portable_charger_booking"
}
}
}
}
everything i have added like in manifest intent-filter but the main problem is i am not getting any logs when notification is coming
I am expecting on click on notification my app will open
Sachin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.