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

问题描述:

我正在尝试为我们的android应用实施Firebase通知。如何使用Firebase通知打开动态链接?

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

但是,我不能找出一种方式发送动态链接的通知(以便点击通知时,打开某个动态链接)。我只能看到发送文字通知的选项。

是否有任何解决方法或者这是FCM的限制?

您将不得不实现服务器端发送的通知与自定义数据,因为目前控制台不支持它。 (使用自定义键值对不会工作,因为当您的应用程序处于后台模式时,通知将不会深入链接)。在此处阅读更多信息:https://firebase.google.com/docs/cloud-messaging/server

一旦拥有了自己的App Server,就可以将Deep Link URL包含到通知的自定义数据部分。

在您的FirebaseMessagingService实施中,您需要查看有效负载并从那里获取URL,然后创建一个使用该Deep Link 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()); 
} 

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,你不能处理的任何链接。

+0

谢谢我们在我们公司使用完全一样的 – PedroAGSantos