How to open dynamic links using Firebase notifications?

不羁岁月 提交于 2019-12-04 14:04:46

问题


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.

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


回答1:


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

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

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.

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:

@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();
    }
}

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.



来源:https://stackoverflow.com/questions/41439291/how-to-open-dynamic-links-using-firebase-notifications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!