我正在尝试为我们的android应用程序实现Firebase通知。
我还在应用程序中实现了动态链接。
但是,我不能想出一种发送带有动态链接的通知的方法(这样在单击通知时,某个动态链接就会打开)。我只能看到发送文本通知的选项。
有没有解决办法,或者这是FCM的一个限制?
发布于 2017-01-03 18:41:14
您必须使用自定义数据实现通知的服务器端发送,因为目前控制台不支持它。(使用自定义的键-值对也不会起作用,因为当你的应用程序处于后台模式时,通知不会深入链接)。点击此处阅读更多信息: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());
}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();
}
}在执行此实现时,与仅将意图设置为带有任何URL的Intent.ACTION_VIEW相比,您也不会打开任何无法处理的链接。
https://stackoverflow.com/questions/41439291
复制相似问题