单击通知FCM时未打开所需的活动 [英] Desired activity not open while clicking on notification FCM
问题描述
我正在FCM上进行演示,如果我单击通知,它将进入通知类并显示通知消息.但是每当我单击通知时,它总是重定向到默认启动器活动,即MainActivity.我见过像我这样的重复问题,但没有适当的解决方案.无论我找到什么解决方案,我都尝试过那些也不起作用的方法.请查看我的意图代码,让我知道我在哪里做错了,或者我想在代码中添加其他内容.
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);
在清单中我也喜欢
android:exported="true"
但仍然每次都重定向到MainActivity.
but still it redirects to MainActivity every time.
推荐答案
您可以尝试以下代码:-
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());
}
}
这篇关于单击通知FCM时未打开所需的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!