1

I am doing a demo on FCM where if i click on notification it should go to notification class and show the notification message. But whenever I click notification it always redirect to default launcher activity i.e MainActivity. I have seen so many duplicate question like me but no where proper solution. Whatever solution I have found I have tried those also not working. Please look into my intent code and let me know where I have done mistake or I am missing anything else to add anything in my code.

Intent resultIntent = new Intent(mCtx, NotificationActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, resultIntent,
        PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_ONE_SHOT);

mBuilder.setContentIntent(pendingIntent);

And in manifest also I have put like

  android:exported="true"

but still it redirects to MainActivity every time.

Sekhar
  • 125
  • 2
  • 9

3 Answers3

0

It's problem with notification payload sent from the backend. FCM will open default launcher activity if the data payload arrives and notification payload will make you launch the custom activity. Right now your onMessageReceived method is not called. For more detail explanation refer this answer

Aseem Sharma
  • 1,205
  • 10
  • 18
  • I am testing from FCM console Aseem – Sekhar Nov 03 '18 at 09:44
  • FCM console sends data payload which cannot be changed, you need to send it using some backend tech like php using phpfiddle, sample code to send push can be found on google just host it on [phpfiddle](https://www.google.co.in/url?sa=t&source=web&rct=j&url=http://phpfiddle.org/&ved=2ahUKEwickdrp4breAhXKL48KHbEAD5EQFjAAegQIBRAD&usg=AOvVaw0xeumCQs6dj0vHN5b7pvoM) and try after that. – Aseem Sharma Nov 04 '18 at 12:46
  • Here is the reference [link](https://gist.github.com/MohammadaliMirhamed/7384b741a5c979eb13633dc6ea1269ce) , use this to upload on phpfiddle and then run it to receive correct type of push data to launch custom activity. – Aseem Sharma Nov 04 '18 at 12:53
0

Follow this, Remove unnecessary code from it. It is from an active project, might help you what is missing.

fun createNormalNotification(notiId: Int, context: Context,
                                 toClass: Class<out Activity>,
                                 title: String, content: String,
                                 smallIcon: Int) {
        val intent = Intent(context, toClass)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        val pendingIntent = PendingIntent.getActivity(context, notiId
                , intent, PendingIntent.FLAG_UPDATE_CURRENT)
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as
                NotificationManager

        creatingNotificationChannel(notificationManager, P.getChannelId(context), P
                .getChannelName(context),
                P.getChannelDescription(context))
        val pattern = longArrayOf(500, 500, 500, 500, 500, 500, 500, 500, 500)
        val mBuilder = NotificationCompat.Builder(context, P.getChannelId(context))
                .setSmallIcon(smallIcon)
                .setContentTitle(title)
                .setContentText(content)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setVibrate(pattern)
                .setAutoCancel(true);

        val notification = mBuilder.build()
        notification.flags = Notification.FLAG_NO_CLEAR or Notification.FLAG_ONGOING_EVENT
        notificationManager.notify(notiId, mBuilder.build());
    }


@TargetApi(Build.VERSION_CODES.O)
private fun creatingNotificationChannel(notificationManager: NotificationManager,
                                            channelId: String, channelName: String,
                                            channelDescription: String) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create the NotificationChannel, but only on API 26+ because
            // the NotificationChannel class is new and not in the support library
            val name: CharSequence = channelName;
            val description = channelDescription
            val channel = NotificationChannel(channelId, name, NotificationManager
                    .IMPORTANCE_DEFAULT);
            channel.description = description;
            // Register the channel with the system

            notificationManager.createNotificationChannel(channel);
        }
    }
Shaon
  • 1,404
  • 14
  • 17
0

You can try below code:-

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private String TAG = getClass().getSimpleName();


@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Log.e(TAG, "FROM>>" + remoteMessage.getFrom());

    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Message Body>>" + remoteMessage.getData());
    }

    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "Message Body>>" + remoteMessage.getNotification().getBody());
        Log.e(TAG, "Message title---------------->>" + remoteMessage.getNotification().getTitle());
        Log.e(TAG, "Message Body----------------->>" + remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
    }
}

private void sendNotification(String tital, String body) {
    Intent intent = null;


    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("Your Notification")
            .setContentText(body)
            .setSound(uri)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(body))
            .setAutoCancel(true);

    Log.e(TAG, "Message Body---" + body);
    Log.e(TAG, "Title--" + tital);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());

 }
}
Marco RS
  • 7,536
  • 3
  • 32
  • 41
Info
  • 1
  • 2