Desired activity not open while clicking on notification FCM

依然范特西╮ 提交于 2021-01-29 02:31:11

问题


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.


回答1:


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




回答2:


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);
        }
    }



回答3:


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());

 }
}


来源:https://stackoverflow.com/questions/53129669/desired-activity-not-open-while-clicking-on-notification-fcm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!