You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Unity iOS通知:应用图标角标计数不显示问题求助

Troubleshooting iOS Badge Count Issues with Firebase Unity Notifications

Hey there! First off—this isn't how Firebase Unity iOS notifications are supposed to work; badge counts should absolutely show up when configured correctly. Let's walk through the key troubleshooting steps to get this fixed:

1. Verify APNs & Xcode Project Setup

iOS badge counts rely on Apple Push Notification service (APNs), so double-check these critical settings in your exported Xcode project:

  • Go to Signing & Capabilities and ensure Push Notifications is added (you'll need a valid Provisioning Profile with push permissions enabled).
  • If you want badge updates to work even when the app is in the background, add Remote notifications under the Background Modes capability.
  • Confirm your GoogleService-Info.plist is properly imported and that the APNS_ENVIRONMENT value matches your build type (use development for testing, production for App Store builds).

2. Check Your Notification Payload

Firebase won't automatically set a badge count unless you explicitly include it in the notification payload:

  • If sending via the Firebase Console: Head to Advanced options > iOS > Add the badge parameter (set it to a number like 1, or use Increment to add to the current count).
  • If sending via Firebase Cloud Messaging (FCM) API: Include the badge field in the APNs payload section:
    {
      "apns": {
        "payload": {
          "aps": {
            "badge": 1
          }
        }
      },
      "notification": {
        "title": "Your Title",
        "body": "Your Message"
      },
      "token": "DEVICE_TOKEN"
    }
    
    Note: Using "badge": 0 will clear the badge entirely.

3. Handle Foreground Notifications in Unity

iOS doesn't automatically update the badge when a notification is received while the app is in the foreground. You'll need to add custom code to handle this:

  • Implement the OnMessageReceived callback in your Firebase Messaging script.
  • Extract the badge count from the notification data, then call iOS's native API to update the badge. You can do this with a simple C#/iOS plugin bridge:
    using UnityEngine;
    using Firebase.Messaging;
    
    public class FirebaseNotificationHandler : MonoBehaviour
    {
        void Start()
        {
            FirebaseMessaging.MessageReceived += OnMessageReceived;
        }
    
        void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            // Extract badge count from the notification
            if (e.Message.Data.TryGetValue("badge", out string badgeStr) && int.TryParse(badgeStr, out int badgeCount))
            {
                // Call iOS native code to update badge
                #if UNITY_IOS && !UNITY_EDITOR
                UpdateiOSBadge(badgeCount);
                #endif
            }
        }
    
        [System.Runtime.InteropServices.DllImport("__Internal")]
        private static extern void UpdateiOSBadge(int count);
    }
    
    Then add this Objective-C code to a .mm file in your Xcode project:
    #import <UIKit/UIKit.h>
    
    extern "C" void UpdateiOSBadge(int count) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];
        });
    }
    

4. Rule Out System & User Settings

Don't overlook the obvious—make sure the user's device allows badge notifications for your app:

  • On the iPhone, go to Settings > Notifications > [Your App] and ensure the Badges toggle is enabled.

5. Update Firebase SDK & Test Edge Cases

  • Make sure you're using the latest version of the Firebase Unity SDK (old versions had known bugs with iOS badge handling).
  • Test both background and foreground scenarios: If badges work in the background but not foreground, the issue is definitely the foreground handling code. If they don't work at all, circle back to payload or APNs setup.

内容的提问来源于stack exchange,提问作者wilk3ns

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:30:50