繁体   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