5

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.

AL.
  • 36,815
  • 10
  • 142
  • 281
Badr
  • 294
  • 1
  • 3
  • 14

6 Answers6

11

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>
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50
3

place intent filter in your Manifest inside activity tag which you want to use click performance in action you have to write same name which you had given in payload fiends.

      <activity name="your activity name" >
         <intent-filter>
             <action  android:name=".Activities.CustomerRebateConfirmation" />
           <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
       <activity>
Alireza Noorali
  • 3,129
  • 2
  • 33
  • 80
android_jain
  • 788
  • 8
  • 19
2

try this in your MainActivity class.

@Override
public void onNewIntent(Intent intent) {
    Bundle extras = intent.getExtras();
    if (extras != null) {
        if (extras.containsKey("type")) {
            String type = extras.getString("type");
            if (type.equals("test type")) {
                Toast.makeText(this, extras.getString("message"), Toast.LENGTH_SHORT).show();
            }

        }
    }
}

I have tried this when i am suffered from this issue. Hope this will be helpful to you.

MessagingServiceClass.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FCM Service";
int mNoti = 2019;
private final int NOTIFICATION_ID = 237;
private static int value = 0;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // TODO: Handle FCM messages here.
    String title = "0";
    String type = "0";
    String message = "0";
    if (remoteMessage.getData().size() > 0) {
        type = remoteMessage.getData().get("type");
        title = remoteMessage.getData().get("title");
        message = remoteMessage.getData().get("message");
        sendNotification(type, title, message);
    }

}

private void sendNotification(String type, String title, String message) {
    Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
            R.mipmap.ic_launcher);

    //AppMethod.setIntegerPreference(getApplicationContext(),NOTIFICATION_ID,)
    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("type", type);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);


    PendingIntent pendingIntent = PendingIntent.getActivity(this, value, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setLargeIcon(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(value, notificationBuilder.build());
    value++;
}

}

Vishal Gedia
  • 207
  • 2
  • 8
2

This question is 2 years old. But is still relevant. This is how you could do it with the firebase console (without `click_action).

When your app is in background onMessageReceived will not be called. But when you get a notification while your app is in background, you will get an Intent along with the custom data you specify in the firebase console.

So when the user taps the notification the intent will then be executed to open your launcher activity. You can check for the data by getIntent().hasExtra("key") in your launcher activity. Where the "key" is whatever key you specify in the console.

Check if you have that "key" then, you can make another Intent and call startActivity

Here is an implementation of mine,

On my SplashActivity > OnCreate (This method Looks best if you have a splash screen as the launcher activity):

if (getIntent().hasExtra("key")){

      Intent intent = new Intent(this, TargetActivity.class);
      startActivity(intent);
      finish();

} else {
      startActivity(new Intent(this, MainActivity.class));
      finish();
}

This will just start the TargetActivity. You can add any functionality to this as per your wish :)

Faisal Shaikh
  • 3,900
  • 5
  • 40
  • 77
0

In your addFlags, try add Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK. It works for me.

Zarul Izham
  • 569
  • 5
  • 17
0

in onMessageReceived() create

String click_action = remoteMessage.getNotification().getClickAction();

and replace your intent with this

   Intent customerRebate = new Intent(click_action);
Vega
  • 27,856
  • 27
  • 95
  • 103
emad pirayesh
  • 654
  • 7
  • 12