Search code examples
androidandroid-intentandroid-pendingintentnotificationmanager

PendingIntent not working with FCM notification


i want to open Detail activity on notification click event. Here is the PendingIntent and Notification set class. I added addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) also added PendingIntent.FLAG_UPDATE_CURRENT. I searched all topics on site i found this solutions i tried all of them.

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // TODO(developer): Handle FCM messages here.
        // If the application is in the foreground handle both data and notification messages here.
        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());

        ArrayList<String> notificationData = new ArrayList<String >(remoteMessage.getData().values());
        String fortuneID = notificationData.get(0);
        sendNotification(remoteMessage.getNotification().getBody(),fortuneID);
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody,String notificationID) {

        Intent configureIntent = new Intent(getApplicationContext(), FalDetayActivity.class);
        configureIntent.putExtra("extra", "123123");
        configureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        configureIntent.setAction("dummy_unique_action_identifyer" + "123123");
        int dummyuniqueInt = new Random().nextInt(543254);
        PendingIntent pendingClearScreenIntent = PendingIntent.getBroadcast(getApplicationContext(), dummyuniqueInt, configureIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("myApp")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingClearScreenIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(1 /* ID of notification */, notificationBuilder.build());
    }
}

Here is the getBundle part MainActivity

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String quote = getIntent().getStringExtra("extra");

        ...
}

Another problem i want to open FalDetailActivity but always open MainActiviy and getIntent() always return null. Probably, my PendingIntent not set i did not found error please check this code. Thanks...

UPDATE

I update getBroadcast to getActivity and remove something

        Intent configureIntent = new Intent(getApplicationContext(), FalDetayActivity.class);
        configureIntent.putExtra("extra", "123123");
        configureIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingClearScreenIntent = PendingIntent.getActivity(getApplicationContext(), 0, configureIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Solution

  • Finally i found the solution : https://firebase.google.com/docs/notifications/android/console-audience

    When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

    This includes messages that contain both notification and data payload. In these cases, 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.