如何使用Firebase通知打开动态链接?

San*_*p C 3 android deep-linking firebase firebase-cloud-messaging firebase-dynamic-links

我正在尝试为我们的Android应用实施Firebase通知.

我还在应用程序中实现了动态链接.

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

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

rig*_*roo 13

您必须使用自定义数据实现服务器端发送通知,因为当前控制台不支持它.(使用自定义键值对无法正常工作,因为当您的应用处于后台模式时,通知不会深层链接).在此处阅读更多内容:https://firebase.google.com/docs/cloud-messaging/server

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

在您的FirebaseMessagingService实现中,您需要查看有效负载并从中获取URL,创建使用该深层链接URL的自定义意图.

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

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());
}
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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