Cant see action buttons on notification when app i

2019-07-31 01:46发布

问题:

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..

回答1:

In FCM there are three type of messages you can send

1. Notification message

FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf.

2.Data message

Client app is responsible for processing data messages. Data messages have only custom key-value pairs.

Use data messages when you want to process the messages on your client app.

3. Both Notification and Data

Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

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

Authorization:key=yourserverkey

Content-Type: application/json

Payload

{"data": "extra data to be send",
 "to" : "devicetoken"
}

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