Push Notification with action button is not working - android

i have used the fcm for notification and its coming correctly, but when i try to add add buttons in notification its not showing.
my code
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New Message From Film")
.setContentText(messageBody)
.setAutoCancel(true)
.addAction(R.drawable.pp, "Accept",pendingIntent)
.addAction(R.drawable.pp, "Reject",pendingIntent)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());

You forgot to build use method build() in last
edited code
Builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New Message From Film")
.setContentText(messageBody)
.setAutoCancel(true)
.addAction(R.drawable.pp, "Accept",pendingIntent)
.addAction(R.drawable.pp, "Reject",pendingIntent)
.setContentIntent(pendingIntent);
notification
final Notification notification = notificationBuilder.build();
mNotificationManager.notify(notificationId, notification);

Related

Notification couldn't disappear after click, and Notification setAutoCancel(true) doesn't work

Notification couldn't disappear after click, and Notification setAutoCancel(true) doesn't work. The following is my code:
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New Message")
.setDefaults(Notification.DEFAULT_ALL)
.setContentText("New Question")
.setAutoCancel(true);
notifyManager.notify(1, builder.build());
Try setContentIntent():
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, new Intent(), 0);
NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("New Message")
.setDefaults(Notification.DEFAULT_ALL)
.setContentText("New Question")
.setContentIntent(pendingIntent)
.setAutoCancel(true);
notifyManager.notify(1, builder.build());

My app is killed and getting push notification from firebase using FirebaseMessagingService. On push notification click i want to open the app

Intent intent = new Intent(getApplicationContext(), SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |Intent.FLAG_ACTIVITY_SINGLE_TOP);
final PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,intent,PendingIntent.FLAG_UPDATE_CURRENT );
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(AppConstants.NOTIFICATION_ID, notification);

How to send Notification through Firebase console including Custom data?

I have to send a URL in notification and I want upon clicking the notification, the link will be opened immediately by Chrome. Currently, when I click the notification, it only opens my app.
What changes do I have to implement in this code for my desired behavior?
private void sendNotification(String messageBody, String title) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}

Multiple notification click launching a same activity android

I have a notification set up :
private void sendNotificationForCancellation(String appointmentId, String title, String message) {
//AppController.getSharedPreferences().edit().putString(Constants.CALL_CASE_ID, notifObject.getCaseDetails().getCaseID()).commit();
Intent intent = new Intent(this, ActCallRequestsDetail.class);
intent.putExtra("appointment_id", appointmentId);
intent.putExtra("isAppIdAvailable", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_note_plus)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon))
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Random generator = new Random();
notificationManager.notify(generator.nextInt(), notificationBuilder.build());
}
On click of notification it opens a activity
Scenario-1:
It is working fine when I have a single notification.
.
Scenario-2:
Assume 2 notifications generated from above notification setup first
notification click, it is opening the activity Now i am there in the
opened activity(ActCallRequestsDetail.class) When I click the
second notification, nothing happens the activity
(ActCallRequestsDetail.class) is not refreshed
.
How to solve the Scenario-2
Solution was just adding a new ID for PendingIntent reference
private void sendNotificationForCancellation(String appointmentId, String title, String message) {
Random generator = new Random();
//AppController.getSharedPreferences().edit().putString(Constants.CALL_CASE_ID, notifObject.getCaseDetails().getCaseID()).commit();
Intent intent = new Intent(this, ActCallRequestsDetail.class);
intent.putExtra("appointment_id", appointmentId);
intent.putExtra("isAppIdAvailable", true);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, generator.nextInt(), intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_note_plus)
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.app_icon))
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(generator.nextInt(), notificationBuilder.build());
}

Preview Notification on android

I'm using this code to show a notification to user from code :
Intent intent = new Intent(this, this.getClass());
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.icon)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon))
.setContentTitle("Notification!")
.setContentText("Hello word")
.setContentIntent(pi)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
This is juste adding the notification in the notification screen (and also the icon of my appliaction in the status bar)
But how can I have a preview of this notification on top of screen ?

Categories

Resources