aksafan / yii2-fcm-both-api

Yii2 Extension for sending push notification with both Firebase Cloud Messaging (FCM) HTTP Server Protocols (APIs).
MIT License
14 stars 13 forks source link

Android channel id #9

Closed LLouzada closed 1 year ago

LLouzada commented 1 year ago

Is it possible to set the channel id for android? I tried adding "android_channel_id" to setAndroidConfig, 'notification' => [ 'title' => $title, 'body' => $message, 'android_channel_id' => $type, ], But this seems not to work.

bc-anton-k commented 1 year ago

Hello @LLouzada1,

Thank you for asking!

You can add any Android specific keys for notification messages, as well as android_channel_id. Just pass array of configs to setAndroidConfig() method with android_channel_id key-value under notification section (see example below).

/** @var \aksafan\fcm\source\responses\apiV1\TokenResponse $result */
$result = Yii::$app
    ->fcm
    ->createRequest()
    ->setTarget(\aksafan\fcm\source\builders\apiV1\MessageOptionsBuilder::TOKEN, 'your_token')
    ->setAndroidConfig([
        'ttl' => '3600s',
        'priority' => 'normal',
        'notification' => [
            'title' => 'Android Title',
            'body' => 'Android Description.',
            'icon' => 'stock_ticker_update',
            'color' => '#ff0000',
            'android_channel_id' => '123456',
        ],
    ])
    ->send();

Make sure to create a channel with this channel ID before any notification with this channel ID is received. Ref to official documentation (Table 2b. Android — keys for notification messages):

image
LLouzada commented 1 year ago

Hi @bc-anton-k , thank you for your reply!

Me and my team managed to make it work using the key channel_id, not android_channel_id, as mentined in here https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#AndroidNotification.

But I guess the way you showed should work too, anyway, thank you for your time :)