简体   繁体   中英

Flutter: How to define notification channel in firebase_messaging package?

I am sending this json to cloud messaging (with proper headers) and want to get notification on my device, but it does not appear on my phone. I think that maybe i am missing channel id initialization for Android?

*Additional info

  • If i send "notification" payload with title or body it is receiving, but i dont want to use "notification" payload. I want to have "data" payload only
{
"to" : "my device token",
"mutable_content" : true,
"content_available": true,
    "data" : {
        "content": {
            "id": 100,
            "channelKey": "basic_channel",
            "title": "Huston!\nThe eagle has landed!",
            "body": "A small step for a man, but a giant leap to Flutter's community!",
            "notificationLayout": "BigPicture",
            "largeIcon": "https://media.fstatic.com/kdNpUx4VBicwDuRBnhBrNmVsaKU=/full-fit-in/290x478/media/artists/avatar/2013/08/neil-i-armstrong_a39978.jpeg",
            "bigPicture": "https://www.dw.com/image/49519617_303.jpg",
            "showWhen": true,
            "autoCancel": true,
            "privacy": "Private"
        },
        "actionButtons": [
            {
                "key": "REPLY",
                "label": "Reply",
                "autoCancel": true,
                "buttonType":  "InputField"
            },
            {
                "key": "ARCHIVE",
                "label": "Archive",
                "autoCancel": true
            }
        ]
    }
}

In order to define a notification channel you have to go to your AndroidManifest.xml and within the application component add this meta:

<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>

then in your strings.xml file put the channel id like this:

<string name='default_notification_channel_id'>Channel ID</string>

After doing this, you can define your channel id in the FCM Console.

As simple as this. Just put your android channel name here below and it works with a charm:

"notification": {
            "body": "Test notification",
            "title": "Test Test Test",
            "click_action": "FLUTTER_NOTIFICATION_CLICK",
           
            "sound": "your_custom_sound" 
            "android_channel_id": "channel_id_youcreated",
},

          'to':
              "",
        },

Sound file name is not necessary in the above given notification payload if you assigned that sound to your notification channel thru MainActivity.kt or java file. However, it is necessary for older Android versions as they will use the sound file directly. Put your file inside res/raw folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM