1

我正在嘗試爲我們的android應用實施Firebase通知。如何使用Firebase通知打開動態鏈接?

我也在應用程序中實現了動態鏈接。

但是,我不能找出一種方式發送動態鏈接的通知(以便點擊通知時,打開某個動態鏈接)。我只能看到發送文字通知的選項。

是否有任何解決方法或者這是FCM的限制?

回答

7

您將不得不實現服務器端發送的通知與自定義數據,因爲目前控制檯不支持它。 (使用自定義鍵值對不會工作,因爲當您的應用程序處於後臺模式時,通知將不會深入鏈接)。在此處閱讀更多信息: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

相關問題