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

时间:2019-06-12 02:51:05

标签: c# android firebase unity3d firebase-cloud-messaging

我正在实施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);
}

/** * 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?

1 个答案:

答案 0 :(得分:1)

带有onCreate的代码在Configuring a custom entry point Activity下列出。仅当您遵循Unity本身关于Extending the UnityPlayerActivity的说明时才需要这样做(通常应该知道何时完成此操作,尽管有时插件(如该插件)必须自己执行此功能。< / p>

要处理深层链接,您必须为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>

您不需要键值对。要查看如何接收消息,除了已经找到的文档之外,您还应该查看sample application

要注意的重要一点是,它在执行任何操作之前都会检查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

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