簡體   English   中英

如何使用Unity3D的Deep Link設置Firebase推送通知

[英]How to set up Firebase Push Notification with Deep Link with Unity3D

我正在實施Firebase Cloud Messaging,以將推送通知發送到適用於Android和iOS設備的Unity項目。 我現在正在使用Android進行調試。 我在設備上收到推送通知,但我想對其進行設置,以便在單擊“推送通知”到應用程序上特定頁面后,可以發送和接收引導我的深層鏈接。

我正在遵循Google的《手冊》( https://firebase.google.com/docs/cloud-messaging/unity/client ),但是不確定我是否理解正確。 他們建議配置自定義入口點,以便在我的活動中添加給定代碼:

/** * Workaround for when a message is sent containing both a Data and Notification payload. * * When the app is in the background, if a message with both a data and notification payload is * receieved the data payload is stored on the Intent passed to onNewIntent. By default, that * intent does not get set as the Intent that started the app, so when the app comes back online * it doesn't see a new FCM message to respond to. As a workaround, we override onNewIntent so * that it sends the intent to the MessageForwardingService which forwards the message to the * FirebaseMessagingService which in turn sends the message to the application. */ @Override protected void onNewIntent(Intent intent) { Intent message = new Intent(this, MessageForwardingService.class); message.setAction(MessageForwardingService.ACTION_REMOTE_INTENT); message.putExtras(intent); message.setData(intent.getData()); startService(message); }

 /** * Dispose of the mUnityPlayer when restarting the app. * * This ensures that when the app starts up again it does not s tart with stale data. */ @Override protected void onCreate(Bundle savedInstanceState) { if (mUnityPlayer != null) { mUnityPlayer.quit(); mUnityPlayer = null; } super.onCreate(savedInstanceState); } 

我收到以下問題:這是本地Android代碼嗎? 還是我必須在Unity的某個地方添加它? 那iOS呢?

閱讀本節:在Android上使用深層鏈接處理消息我將代碼(調整域)添加到我的Android清單中。

另外,我不明白如何從Firebase控制台發送DeepLink並使用Unity處理它。 我是否必須將其設置為鍵值對? 用什么鑰匙? 我如何處理鍵值對/或者,如果不是,我如何處理一般的deepLink?

配置自定義入口點Activity下列出了帶有onCreate的代碼。 僅當您按照從Unity本身擴展UnityPlayerActivity的說明進行操作時才需要這樣做 (通常應該知道何時完成此操作,盡管有時插件(如該插件)必須自己執行才能起作用。

為了處理深層鏈接,您必須為ApplicationManifest.xml修改意圖過濾器。 導入Firebase Messaging插件后,您應該在Assets / Plugins / AndroidManifest.xml下擁有一個AndroidManifest.xml。 從6.1.1開始,它看起來像這樣:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.google.firebase.unity.database.testapp.patrick" android:versionCode="1" android:versionName="1.0">
  <application android:label="@string/app_name" android:icon="@drawable/app_icon">
    <!-- The MessagingUnityPlayerActivity is a class that extends
         UnityPlayerActivity to work around a known issue when receiving
         notification data payloads in the background. -->
    <activity android:name="com.google.firebase.MessagingUnityPlayerActivity" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
    <service android:name="com.google.firebase.messaging.MessageForwardingService" android:exported="false" />
  </application>
</manifest>

只需將您的意圖過濾器放在那里,如果您的域名是example.com,則類似以下內容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.google.firebase.unity.database.testapp.patrick" android:versionCode="1" android:versionName="1.0">
  <application android:label="@string/app_name" android:icon="@drawable/app_icon">
    <!-- The MessagingUnityPlayerActivity is a class that extends
         UnityPlayerActivity to work around a known issue when receiving
         notification data payloads in the background. -->
    <activity android:name="com.google.firebase.MessagingUnityPlayerActivity" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />

        <!-- stuff for deep links -->
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
        <data android:host="*.example.com" android:scheme="http"/>
        <data android:host="*.example.com" android:scheme="https"/>
      </intent-filter>
      <meta-data android:name="unityplayer.UnityActivity" android:value="true" />
    </activity>
    <service android:name="com.google.firebase.messaging.MessageForwardingService" android:exported="false" />
  </application>
</manifest>

您不需要鍵值對。 要查看如何接收消息,除了已經找到的文檔外,您還應該查看示例應用程序

需要注意的重要一點是,它在執行任何操作之前都會檢查firebase依賴項:

      Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
        dependencyStatus = task.Result;
        if (dependencyStatus == Firebase.DependencyStatus.Available) {
          InitializeFirebase();
        } else {
          Debug.LogError(
            "Could not resolve all Firebase dependencies: " + dependencyStatus);
        }
      });

在初始化函數中,將為進入的消息注冊處理程序:

      Firebase.Messaging.FirebaseMessaging.MessageReceived += OnMessageReceived;
      Firebase.Messaging.FirebaseMessaging.TokenReceived += OnTokenReceived;

(如果您這樣做的話,樣本也在此訂閱主題)

並要求獲得接收通知的權限:

      Firebase.Messaging.FirebaseMessaging.RequestPermissionAsync().ContinueWithOnMainThread(
        task => {
          LogTaskCompletion(task, "RequestPermissionAsync");
        }
      );

示例OnMessageReceived是超級通用的。 您可以查看如何僅接收標題/正文來接收消息:

      if (notification != null) {
        DebugLog("title: " + notification.Title);
        DebugLog("body: " + notification.Body);
        var android = notification.Android;
        if (android != null) {
            DebugLog("android channel_id: " + android.ChannelId);
        }
      }

或者是您詢問的鍵/值數組:

      if (e.Message.Data.Count > 0) {
        DebugLog("data:");
        foreach (System.Collections.Generic.KeyValuePair<string, string> iter in
                 e.Message.Data) {
          DebugLog("  " + iter.Key + ": " + iter.Value);
        }
      }

在iOS上,您只需要設置APN內容即可: https : //firebase.google.com/docs/cloud-messaging/ios/certs

讓我知道您是否被卡住了!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM