如何在Firebase Cloud Messaging (FCM)中使用深度链接?

3

如何在FCM中使用深度链接?主要目的是让用户点击来自FCM的通知后打开应用程序中的特定页面。

我找不到可以插入链接(onelink,dynamic link等)的方法/位置。当前,在点击通知后,应用程序会打开最后一个活动窗口。

1个回答

1

FCM有2种消息类型通知消息数据消息。你不能使用深度链接来处理通知消息,因为它不会调用应用程序中的处理程序,无法做出反应。

相反,发送一个数据消息,并在onMessageReceived中生成自己的通知。查看实现onMessageReceived的文档

文档链接到一个文件,其中显示了创建通知的示例,他们定义了以下方法:

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_stat_ic_notification)
                        .setContentTitle(getString(R.string.fcm_message))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

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

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

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

您会注意到示例创建了一个pendingIntent。如果您想创建一个显式深度链接,您需要修改它以调用.setComponentName(DestinationActivity.class)。有关此内容的更多文档,请参阅为目标创建深度链接


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接