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..
In FCM there are three type of messages you can send
1. Notification message
2.Data message
3. Both Notification and Data
So if you want to handle message on client side better you go with Data Message
here you get all details
For send Data Message
For Data Message you have to call post service using following URL
https://fcm.googleapis.com/fcm/send
Headers
Payload
Note: replace "to" with "registration_ids" : ["1","2","3","4","5","6"] for multiple devices
In Application onMessageReceived you can get data message using remoteMessage.getData()
For detail already mentioned this https://firebase.google.com/docs/cloud-messaging/concept-options
Here you also check to send notification to Topics using data message https://firebase.google.com/docs/cloud-messaging/android/topic-messaging