当应用不是 运行 时,如何为 data-payload firebase 通知定义标题?

How do I define a title for a data-payload firebase notification when the app is not running?

我正在将 GCM-based 消息传递应用程序转换为 Firebase。消息正在使用 data-payload 格式发送到应用程序。如果我的应用程序是 运行,无论是在前台还是在后台,OnMessageRecieved 中的代码都会运行并设置通知标题。但是,如果收到通知时应用程序不是 运行,则它不会显示标题。我试过向数据负载添加标题:

{
    "data": {
        "title": "My Title",
        "message": "Text of the message",
    }
}

并且还尝试按照定义图标的格式在 AndroidManifest.xml 中定义它:

<meta-data android:name="com.google.firebase.messaging.default_notification_title" android:value="My Title"/>

但是这两种方法都不起作用。

    public override void OnMessageReceived(RemoteMessage message)
    {
        try
        {
            SendNotification(message.GetNotification()?.Body, message.Data);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

    void SendNotification(string messageBody, IDictionary<string, string> data)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        foreach (string key in data.Keys)
        {
            if (key == "message")
                messageBody = data[key];
            else
                intent.PutExtra(key, data[key]);
        }
        var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);

        var notificationBuilder = new Notification.Builder(this)
            .SetSmallIcon(Resource.Drawable.smallicon)
            .SetContentTitle(AppInfo.DisplayName)
            .SetContentText(messageBody)
            .SetAutoCancel(true)
            .SetDefaults(NotificationDefaults.Sound | NotificationDefaults.Vibrate)
            .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManager.FromContext(this);
        notificationManager.Notify(0, notificationBuilder.Build());
    }

原来 OnMessageRecieved 是 运行,问题出在标题本身的设置上:

.SetContentTitle(AppInfo.DisplayName)

AppInfo.DisplayName是在MainActivity的OnCreate方法中设置的。我将变量替换为:

.SetContentTitle(ApplicationInfo.LoadLabel(PackageManager))

并且当应用程序未 运行ning 时,在收到的通知中显示应用程序名称作为标题。感谢 BobSnyder 为我指明了正确的方向!