Badge is not set for Android

Created on 24 May 2024, 7 months ago
Updated 21 June 2024, 6 months ago

Problem/Motivation

The $badge parameter allows you to set the badge count but it doesn't work on Android, only iOS.

🐛 Bug report
Status

Needs work

Version

7.0

Component

Code

Created by

🇯🇵Japan ptmkenny

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @ptmkenny
  • 🇭🇺Hungary kecsot Hungary

    You are looking for:

    $config = AndroidConfig::fromArray([
        'notification' => [
            'notification_count' => $badge_count
        ]
    ]);
  • Merge request !34add badge count for android → (Open) created by ptmkenny
  • 🇯🇵Japan ptmkenny

    @kescot Thanks for pointing me on the right path! According to the docs/kreait's type checker, the notification_count doesn't need to be wrapped in a notification array:

          $message = $message->withAndroidConfig(AndroidConfig::fromArray([
            'notification_count' => $badge_count,
          ]));
    
  • 🇭🇺Hungary kecsot Hungary

    Are you sure?

    * @phpstan-type AndroidConfigShape array{
    ...
    *      ttl?: positive-int|non-empty-string|null,
     *     notification?: AndroidNotificationShape,
    ...
     * }
    * @phpstan-type AndroidNotificationShape array{
    ...
     *     notification_count?: positive-int,
    ...
     * }
    
        /**
         * @param AndroidConfigShape $config
         *
         * @throws InvalidArgument
         */
        public static function fromArray(array $config): self
        {
            if (array_key_exists('ttl', $config) && $config['ttl'] !== null) {
                $config['ttl'] = self::ensureValidTtl($config['ttl']);
            }
    
            return new self($config);
        }
    

    It looks like it should be wrapped.

    Just like:

        public function withNotificationPriority(string $notificationPriority): self
        {
            $config = clone $this;
    
            $config->config['notification'] ??= [];
            $config->config['notification']['notification_priority'] = $notificationPriority;
    
            return $config;
        }
  • 🇯🇵Japan ptmkenny

    I haven't tested yet in the cloud because I have some other fixes I have to do locally before I can test this, but when I wrapped it in notification, PHPStorm gave me this error:

    phpstan: Parameter #1 $config of static method Kreait\Firebase\Messaging\AndroidConfig::fromArray() expects array{collapse_key?: non-empty-string, priority?: 'high'|'normal', ttl?: int<1, max>|non-empty-string|null, restricted_package_name?: non-empty-string, data?: array<non-empty-string, non-empty-string>, notification?: array{title?: non-empty-string, body?: non-empty-string, icon?: non-empty-string, color?: non-empty-string, sound?: non-empty-string, click_action?: non-empty-string, body_loc_key?: non-empty-string, body_loc_args?: array<int, non-empty-string>, ...}, fcm_options?: array{analytics_label?: non-empty-string}, direct_boot_ok?: bool}, array{notification: array{notification_count: int<0, max>}} given.

    I'll definitely test with real push notifications and confirm before committing anything.

  • 🇯🇵Japan ptmkenny

    @kecsot Testing confirms you are correct, and PHPStorm is wrong. Thank you for being patient with me.

    The badge count now gets set, but in Android, the set badge count is being added to the existing count, whereas in iOS, the set badge count becomes the badge count. This inconsistent behavior is confusing, so I will try to find a way to unify it.

  • Status changed to Needs work 6 months ago
Production build 0.71.5 2024