humhub / app

20 stars 8 forks source link

Notifications #8

Closed luke- closed 1 year ago

luke- commented 1 year ago

There is no dedicated documentation for push.humhub.com , but basically it just sends notifications via the Firebase SDK. as described here: https://firebase.flutter.dev/docs/messaging/notifications/#via-admin-sdks

To keep it simple I would implement it using following scheme:

1. Register App to Firebase Push Proxy Service

The mobile app will receive a new Javascript channel message analogous to showOpener, hideOpener with the name registerFcmDevice. Maybe we change the message format from a simple string to a object. e.g.

{type: 'showOpener'}
{type: 'hideOpener'}
{type: 'registerFcmDevice', url: 'https://sometest12356.humhub.com/fcm-push/token/update' }

Once the a message of type registerFcmDevices is received, the App must create a POST HTTP request to the url given in the message url and send the Firebase Device token with POST attribute name token. Usually HumHub will trigger this channel message right after the hideOpener message.

2. Handling incoming messages

At the beginning we will send the same notifications as currently for Web/PWA to the Flutter App.

The notification will contain the following values: title, body, imageUrl, link.

When the notification is received, body should be displayed. When clicked, 'link' should be opened in the WebView.

PrimozRatej commented 1 year ago

Push notifications were succesfully implemented. DEMO: https://user-images.githubusercontent.com/10835179/220153198-c8af189d-20c6-446f-ab6d-e01e8d1c60ff.mp4

If we opt to use a single channel (e.g. "flutterChannel") to transmit all types of messages between the WebView and the Web application, I recommend devising a structure similar to...

image

There is another option I'm more in favor of is to define separate channels for different types of communications. One could be openerChannel, for registering Device FCM token could be registerDeviceFcmChannel

PrimozRatej commented 1 year ago

Currently, the implementation of push notifications is only compatible with Push Service (Beta). If the user decides to use their own Firebase instance for push notifications, it will not work at the moment. However, there may be a workaround. The general procedure would be sending the configuration values from the WebView to the device if the user has defined their own Firebase instance. image

Then setting up the Firebase instance manually and overriding the def. values. For the time being, we are using the recommended method with google-service.json.

luke- commented 1 year ago

@PrimozRatej I've just changed the channel messages to

{type: 'showOpener'}
{type: 'hideOpener'}
{type: 'registerFcmDevice', url: 'https://sometest12356.humhub.com/fcm-push/token/update' }

I think one channel should be ok.


I don't get the push notifications working on my phone. I've updated the App to Version 0.1.20 and the firebase device token was successfully stored using the Debug Page.

When triggering a test notification it's not pushed / handled by the mobile app.

Any idea?

PrimozRatej commented 1 year ago

Interesting I just tested the flow it works as expected. I get an email, and a push to my phone and on emulator. This is what I have configured inside my User account notifications settings.

image

PrimozRatej commented 1 year ago

PS: Also test it on Google play internet testing track release 38 (0.1.20) notifications seems to work.

luke- commented 1 year ago

@PrimozRatej:

I have also activated all notifications in the humhub settings.

Steps:

To Test Push Notification at all, I've logged in on my mobile with Chrome to the sometesthumhub

After some time I receive 3 notifications (Web, Email, PWA (firebase push)) but no Push Notification from App

PrimozRatej commented 1 year ago

@luke- It appears that the push notifications are not providing any data, which means we are currently unable to handle the redirection upon tapping on them. image

luke- commented 1 year ago

@PrimozRatej For the PWA, the push notification should contains a URL. However, it may be that we have to pass the URL differently for the mobile app.

Should I add a url attribute here? https://firebase-php.readthedocs.io/en/stable/cloud-messaging.html#adding-data

PrimozRatej commented 1 year ago

Yes that's it 👍

luke- commented 1 year ago

@PrimozRatej I've added that data field

luke- commented 1 year ago

@PrimozRatej The number of push notifications is currently displayed in the app icon. It would be better if the app icon always displayed the number of HumHub notifications. Is that possible?

Ideally we can update these app icon number

image vs image

Should we open a new issue for it und postpone it to a later milestone? e.g. 0.4?

PrimozRatej commented 1 year ago

I think the first two options will be adequate. For push notifications, we can include the current badge count of a user in the notification's data, similar to how we handle URLs. When the app is open, we can listen for changes in the badge number within the WebView and use a flutterChannel to update the badge count. Updating the badge count using FlutterAppBadger.updateBadgeCount(newCount) is straightforward. However, I do have concerns about the scheduled updates, which may only work when the app is running in the foreground or background and not when it is terminated.

This feature can be implemented within the scope of this milestone. It is a relatively quick addition.

luke- commented 1 year ago

@PrimozRatej Ok great. Lets start with the first two options:

  1. I`ve added to the Firebase Notification the current amount of open notifications:

    ->withData(['url' => $url, 'notificationCount' => $notificationCount]);
  2. There is now a new flutterChannel message updateNotificationCount. I've also added a Test button here:

image

https://sometestproject12345.humhub.com/mobile-app/index

  1. For the third option, I've added an issue with a new idea for a later milestone: https://github.com/luke-/app/issues/52
PrimozRatej commented 1 year ago

I was testing a push notification for both of those features, but it appears that they are not functioning. Would you mind examining the issue? I do get an email notification but the push is not sent.

luke- commented 1 year ago

@PrimozRatej Oh yes, I introduced an bug here. The problem is now fixed.

PrimozRatej commented 1 year ago

I am unsure if this is FCM specific or a typo, the key that comes with notification count is lowercase notificationcount. If that's a FCM I would go with notification_count

luke- commented 1 year ago

@PrimozRatej I checked it and the key name wascorrect in CamelCase. So it seems to be FCM limitation. If changed it to "notification_count" as you suggested.