pushy / pushy-flutter

The official Pushy SDK for Flutter apps.
Apache License 2.0
21 stars 19 forks source link

Support for Notification Channel on Android #67

Closed bawahakim closed 4 months ago

bawahakim commented 4 months ago

Hello,

Is there support for notification channel on flutter Android? I see a method in the native sdk, but not for flutter.

Thanks!

pushy commented 4 months ago

Hi @bawahakim, Thanks for reaching out. We'd be glad to assist.

Absolutely, the Pushy Flutter SDK automatically takes care of creating and assigning a notification channel to incoming notifications: https://github.com/pushy/pushy-flutter/blob/3d30ef71cec51859115e82cf1a24a90e210aa6a2/android/src/main/java/me/pushy/sdk/flutter/PushyPlugin.java#L566

This happens automatically when you call Pushy.notify() in your backgroundNotificationListener():

@pragma('vm:entry-point')
void backgroundNotificationListener(Map<String, dynamic> data) {
    // Print notification payload data
    print('Received notification: $data');

    // Notification title
    String notificationTitle = 'MyApp';

    // Attempt to extract the "message" property from the payload: {"message":"Hello World!"}
    String notificationText = data['message'] ?? 'Hello World!';

    // Android: Displays a system notification
    // iOS: Displays an alert dialog
    Pushy.notify(notificationTitle, notificationText, data);

    // Clear iOS app badge number
    Pushy.clearBadge();
}

Please let us know if there's anything else we can help with.

bawahakim commented 4 months ago

Thanks! It's great that it automatically happens, but it seems like it doesn't give us flexibility to add our own channel name, description and group (and other smaller settings like LED color). Currently I only see "Push Notifications" category under "Other", even though I sent multiple notifications already. And also no way to localize the content.

pushy commented 4 months ago

Hi @bawahakim, That's correct, the default implementation does not allow customizing the notification channel name, description, nor group, or other settings, as you mentioned.

To customize the Android notification channel display and behavior on Flutter, consider using a package called flutter_local_notifications which allows granular configuration of local Android notifications and their channels, instead of calling Pushy.notify(...) in your backgroundNotificationListener().

Example code to display a local notification with flutter_local_notifications:

const AndroidNotificationDetails androidPlatformChannelSpecifics =
    AndroidNotificationDetails(
        'your channel id', 'your channel name', 'your channel description',
        importance: Importance.max,
        priority: Priority.high,
        showWhen: false);
const NotificationDetails platformChannelSpecifics =
    NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
    0, 'plain title', 'plain body', platformChannelSpecifics,
    payload: 'item x');

Please refer to the docs for configuring custom notification icons and sounds.

To migrate to flutter_local_notifications, remove the following line of code in your backgroundNotificationListener() and invoke the plugin instead:

Pushy.notify(notificationTitle, notificationText, data);

Please give it a try, and let us know if you face any issues.