I have added two actions to the notification i.e. accept and reject. I can see both when the app is in foreground. But I cant see the actions when app is in background.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private String mOrderId, mBillId;
private Boolean mUpdateNotification;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.describeContents());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
//get data from server notification.
sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
}
//send notification
private void sendNotification(String messageBody, String title) {
Intent intent = new Intent();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500, 500, 500, 500, 500};
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.login_logo_1)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setSound(defaultSoundUri)
.addAction(R.string.accept,getString(R.string.accept), pendingIntent)
.addAction(R.string.reject,getString(R.string.reject), pendingIntent)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}
}
Also I want to handle the intents from these actions. For this I have created a class which extends broadcast receiver,but how to call this in an activity?
public class NotificationReceiver extends BroadcastReceiver {
String ACCEPT,REJECT;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(ACCEPT.equals(action)) {
Log.v("Delivery","Accepted");
}
else if(REJECT.equals(action)) {
Log.v("Delivery","Rejected");
}
}
}
Please help. Thank you..