单击通知不会在后台打开 activity

Clicking on notification doesn't open activity on background

我从 firebase 发送通知,而在前台点击通知的应用 运行 将完美运行。但是,如果应用程序在后台运行,则单击通知将不起作用。它不会打开 MainActivity。请帮我看看哪里错了。

onMessageReceived()

if (remoteMessage.getNotification() != null) {
            //Foreground
            Log.d(TAG, "Message Notification Body: " +
                    remoteMessage.getNotification().toString());
            showNotification(remoteMessage.getNotification().getTitle(),
                    String.valueOf(remoteMessage.getNotification().getBody()));
        }
else if (remoteMessage.getData().size() > 0) {
           showNotification(remoteMessage.getData().get("title"),
                    remoteMessage.getData().get("data"));

        }

showNotification()

Intent i = new Intent(this, MainActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    manager.notify(0, builder.build());

请使用click_action.

例如

在您的 android.manifest 文件中

在您注册 activity

的地方添加以下代码
     <activity
                android:name="your activity name">
                <intent-filter>
                    <action android:name="your activity name" />
                   <category android:name="android.intent.category.DEFAULT"/>
               </intent-filter>
   </activity>

您必须在服务器端注册相同的点击操作。

例如

$noti = array
    (
    'icon' => 'your_icon_name',
    'title' => 'title',
    'body' => 'msg',
    'click_action' => 'your activity name comes here'
);