Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FCM Notification click_action not working

I have a problem with FCM, when I receive the notification, I want it to opens specific activity, by the default when I don't add click_action it opens the main activity of the app, but when I add click_action and click on the notification it doesn't perform any action.

Here's the JSON I use in the web service:

{
    "registration_ids": [
        "f4............LL"
    ],
    "notification": {
        "title": "Rebate Confirmation",
        "text": "Please Confirm",
        "sound": "default",
        "click_action": ".Activities.CustomerRebateConfirmation"
    },
    "data": {
        "merchant_id": "20",
        "customer_id": "1",
        "points": "10",
        "totalpoints": "100",
        "message": "Please Confirm",
        "type": "customer_points_rebate_confirmation"
    }
}

and this is my onMessageReceived method:

public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.e(TAG, "From: " + remoteMessage.getFrom());
        customerRebateDetails = new String[5];

        // Check if message contains a data payload.
        if (remoteMessage.getData().size() > 0) {
            Log.e(TAG, "Message data payload: " + remoteMessage.getData());
            Log.e(TAG, "Message notification: " + remoteMessage.getNotification().getBody());

            String type = remoteMessage.getData().get("type");
            String message = remoteMessage.getData().get("message");
            String text = remoteMessage.getNotification().getBody();
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            switch (type){
                case "customer_points_rebate_confirmation":
                    customerRebateDetails[0]  = remoteMessage.getData().get("customer_id");
                    customerRebateDetails[1]  = remoteMessage.getData().get("merchant_id");
                    customerRebateDetails[2]  = remoteMessage.getData().get("points");
                    customerRebateDetails[3]  = remoteMessage.getData().get("totalpoints");
                    Intent customerRebate = new Intent(this, CustomerRebateConfirmation.class);
                    customerRebate.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    customerRebate.putExtra("customer_points_rebate_confirmation", customerRebateDetails);
                    PendingIntent customerRebatePendingIntent = PendingIntent.getActivity(this, 0, customerRebate,
                            PendingIntent.FLAG_ONE_SHOT);
                    NotificationCompat.Builder customerRebateBuilder = new  NotificationCompat.Builder(this)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(message)
                            .setContentText(text)
                            .setSound(defaultSoundUri)
                            .setAutoCancel(true)
                            .setContentIntent(customerRebatePendingIntent);
                    NotificationManager customerRebateManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                    customerRebateManager.notify(0, customerRebateBuilder.build());
                    break;
            }

Does anyone know what is the problem of the implementation?

Note that it works well when the app is in foreground but it's not working when the app is in background.

like image 712
Badr Avatar asked Apr 12 '17 14:04

Badr


1 Answers

Make sure you have added this lines in your CustomerRebateConfirmation activity in Manifest file...

<intent-filter>
     <action android:name=".Activities.CustomerRebateConfirmation" />
     <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
like image 148
Rjz Satvara Avatar answered Oct 20 '22 08:10

Rjz Satvara