如何使用Firebase通知打开动态链接? [英] How to open dynamic links using Firebase notifications?

查看:199
本文介绍了如何使用Firebase通知打开动态链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我们的Android应用程序实现Firebase通知。

I am trying to implement Firebase notifications for our android app.

我也已在应用程序中实现了动态链接。

I have also implemented Dynamic Links in the app.

但是,我无法找到一种通过动态链接发送通知的方法(因此,在单击通知时会打开某个动态链接)。我只能看到发送文本通知的选项。

But, I am not able to figure out a way to send the notification with a dynamic link (so that on clicking the notification, a certain dynamic link is opened). I can only see an option to send a text notification.

有没有解决方法,或者这是FCM的限制吗?

Is there any workaround or is this a limitation of FCM?

推荐答案

您将必须在服务器端实施带有自定义数据的通知发送,因为当前控制台不支持该通知。 (使用自定义键值对将不会起作用,因为当您的应用处于后台模式时,通知将不会进行深层链接)。在此处了解更多信息: https://firebase.google.com/docs/cloud-messaging/server

You will have to implement a server side sending of the notification with a custom data as currently the console doesn't support it. (Using the custom key-value pairs wont work either as when your app is in background mode the notification wont deep link). Read more here: https://firebase.google.com/docs/cloud-messaging/server

拥有自己的App Server之后,便可以将Deep Link URL包含在通知的自定义数据部分中。

Once you have your own App Server, you can then include the Deep Link URL into the custom data section of the notification.

在您的 FirebaseMessagingService 实现中,您需要查看有效负载并从那里获取URL,创建一个自定义

In your FirebaseMessagingService implementation you will need to look at the payload and take the URL from there, create a custom intent that uses that Deep Link URL.

我目前正在使用AirBnb的深度链接调度程序库( https://github.com/airbnb/DeepLinkDispatch )在这种情况下效果很好,因为您可以设置数据和指向DeepLinkActivity的链接,这将为您完成链接处理。在下面的示例中,我将服务器的有效负载转换为名为DeepLinkNotification的对象,其中包含URL字段。

I am currently using AirBnb's deep link dispatcher library (https://github.com/airbnb/DeepLinkDispatch) that works quite well in this situation as you can set the data and the link to the DeepLinkActivity and that will do the link processing for you. In the below example, I convert the payload from the server into an object called DeepLinkNotification and this contains a URL field.

private void sendDeepLinkNotification(final DeepLinkNotification notification) {
    ...
    Intent mainIntent = new Intent(this, DeepLinkActivity.class);
    mainIntent.setAction(Intent.ACTION_VIEW);
    mainIntent.setData(Uri.parse(notification.getUrl()));
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addNextIntent(mainIntent);
    PendingIntent pendingIntent = stackBuilder.getPendingIntent(notificationId, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = buildBasicNotification(notification);
    builder.setContentIntent(pendingIntent);

    notificationManager.notify(notificationId, builder.build());
}

DeepLinkActivity:

DeepLinkActivity:

@DeepLinkHandler
public class DeepLinkActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dispatch();    
    }

    private void dispatch() {
        DeepLinkResult deepLinkResult = DeepLinkDelegate.dispatchFrom(this);
        if (!deepLinkResult.isSuccessful()) {
            Timber.i("Deep link unsuccessful: %s", deepLinkResult.error());
            //do something here to handle links you don't know what to do with
        }
        finish();
    }
}

在执行此实现时,您也不会打开与您将意图仅设置为 Intent.ACTION_VIEW 和任何URL相比,您无法处理的任何链接。

In doing this implementation, you also won't open any links you cant handle compared to if you just set the intent to Intent.ACTION_VIEW with any URL.

这篇关于如何使用Firebase通知打开动态链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆