Search code examples
flutterpush-notificationfirebase-cloud-messaginggetstream-io

Flutter StreamChat SDK : Notifications not showing when the app is closed?


I'm working on a flutter delivery app and I want to integrate chat functionality and I found out that the StreamChat SDK is the best solution for that !

I already integrated the SDK and it's working , so to receive a notification when there is a new message I set up push notifications according to the docs in my flutter applications, when I test it up using stream CLI , I receive the notification and it also works when the app on the background but I don't receive anything when the app is closed and also nothing shows on the dashboard logs ! enter image description here

enter image description here

=====My code ========

 await client.connectUser(
                  User(
                    id: sharedPreferences.getString('username'),
                  ),
                  token,
                );
                var fcm = await FirebaseMessaging.instance.getToken();
                await client.addDevice(fcm, PushProvider.firebase);
                final channel = client.channel(
                  'messaging',
                  id: deliveryData['deliveryNumber'],
                  extraData: {
                    "name": deliveryData['title'],
                  },
                );
                final currentUserId = client.state.user.id;
                client
                    .on(
                  EventType.messageNew,
                  EventType.notificationMessageNew,
                )
                    .listen((event) async {
                  if (event.message?.user?.id == currentUserId) {
                    return;
                  }
                  final flutterLocalNotificationsPlugin =
                      FlutterLocalNotificationsPlugin();
                  final initializationSettingsAndroid =
                      AndroidInitializationSettings('launch_background');
                  final initializationSettings = InitializationSettings(
                    android: initializationSettingsAndroid,
                  );
                  await flutterLocalNotificationsPlugin
                      .initialize(initializationSettings);
                  await flutterLocalNotificationsPlugin.show(
                    event.message.id.hashCode,
                    'New message from : ' + event.message.user.name,
                    event.message.text,
                    NotificationDetails(
                      android: AndroidNotificationDetails(
                        'message channel',
                        'Message channel',
                        'Channel used for showing messages',
                        icon: '@drawable/notification',
                        priority: Priority.high,
                        importance: Importance.high,
                      ),
                    ),
                  );
                });

Solution

  • I found the solution for that , the problem was that I used channel.watch(); instead, I should also add the user to the channel channel.addMembers(["thierry", "josh"]); in order to receive push notifications as it is mentioned in the docs

    Watchers vs Members

    The concepts of watchers vs members can require a bit of clarification:

    • members: a permanent association between a user and a channel. If the user is online and not watching the channel they will receive a notification event, if they are offline they will receive a push notification.

    • watchers: the list of watchers is temporary. It's anyone who is currently watching the channel.

    Being able to send messages, and otherwise engage with a channel as a non-member requires certain permissions. For example, we have pre-configured permissions on our livestream channel type to allow non-members to interact, but in the messaging channel type, only members of the channel can interact.