Badge is not set for Android

Created on 24 May 2024, about 1 year 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

Active

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 → (Merged) 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 about 1 year ago
  • 🇯🇵Japan ptmkenny

    Apparently the badge count not being set correctly for Android is a difference among Android launchers. I'm testing on a 7-year-old phone, so maybe that's why. I'm going to commit this because this is better than nothing.

  • Pipeline finished with Skipped
    13 days ago
    #536953
    • ptmkenny committed 44f8a555 on 7.0.x
      Issue #3449609 by ptmkenny, kecsot: Badge is not set for Android
      
Production build 0.71.5 2024