invertase / notifee

⚛️ A feature rich notifications library for React Native.
https://notifee.app
Apache License 2.0
1.76k stars 201 forks source link

Is there any plan to support MessagingStyle Notification on android like communication Notification on iOS? #1037

Open kdwkr opened 1 month ago

kdwkr commented 1 month ago

I know that already notifee is supporting MessagingStyle Notification.

But it doesn't showing like Communication Notification on iOS

After i searching about it, i found it need to set ShorcutInfo and longLived option of ShortcutInfo to true.

I will upload the patch file for this soon. I hope it can be support on this library

kdwkr commented 1 month ago

For patch @notifee/react-native

Nothing special. just patches for passing options to native code (NotifeeCore)

@notifee-react-native-npm-7.8.2-b551bc6447.patch

kdwkr commented 1 month ago

NotificationManager.java of :notifee_core

    /*
     * A task continuation that sets the notification shortcut, if specified.
     */
    Continuation<NotificationCompat.Builder, NotificationCompat.Builder> shortcutContinuation =
        task -> {
          NotificationCompat.Builder builder = task.getResult();
          if (androidModel.hasShortcutInfo()) {
            Bundle shortcutInfoBundle = androidModel.getShortcutInfo();

            String id = shortcutInfoBundle.getString("id");

            if (id == null) {
              Logger.e(TAG, "Shortcut ID is required for shortcut info");
              return builder;
            }

            ShortcutInfoCompat.Builder shortcutInfoBuilder = new ShortcutInfoCompat.Builder(getApplicationContext(), id);

            String icon = shortcutInfoBundle.getString("icon");
            if (icon != null) {
              Bitmap iconBitmap = null;
              try {
                iconBitmap =
                  Tasks.await(ResourceUtils.getImageBitmapFromUrl(icon), 10, TimeUnit.SECONDS);
              } catch (TimeoutException e) {
                Logger.e(
                  TAG,
                  "Timeout occurred whilst trying to retrieve a icon image: " + icon,
                  e);
              } catch (Exception e) {
                Logger.e(
                  TAG,
                  "An error occurred whilst trying to retrieve a icon image: " + icon,
                  e);
              }

              if (iconBitmap != null) {
                if (androidModel.getCircularLargeIcon()) {
                  iconBitmap = ResourceUtils.getCircularBitmap(iconBitmap);
                }

                shortcutInfoBuilder.setIcon(IconCompat.createWithAdaptiveBitmap(iconBitmap));
              }
            }

            if (shortcutInfoBundle.containsKey("shortLabel")) {
              shortcutInfoBuilder.setShortLabel(shortcutInfoBundle.getString("shortLabel"));
            }

            if (shortcutInfoBundle.containsKey("longLabel")) {
              shortcutInfoBuilder.setLongLabel(shortcutInfoBundle.getString("longLabel"));
            }

            if (shortcutInfoBundle.containsKey("longLived")) {
              shortcutInfoBuilder.setLongLived(shortcutInfoBundle.getBoolean("longLived"));
            }

            if (shortcutInfoBundle.containsKey("action")) {
              NotificationAndroidActionModel actionBundle = NotificationAndroidActionModel.fromBundle(shortcutInfoBundle.getBundle("action"));
              Intent[] intents;
              int targetSdkVersion =
                ContextHolder.getApplicationContext().getApplicationInfo().targetSdkVersion;
              if (targetSdkVersion >= Build.VERSION_CODES.S
                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                intents =
                  // this method is incomplete. i just wrote this code for only testing.
                  NotificationIntent.createIntent(
                    notificationModel.getHashCode(),
                    actionBundle.getPressAction().toBundle(),
                    TYPE_ACTION_PRESS,
                    new String[] {"notification", "pressAction"},
                    notificationModel.toBundle(),
                    actionBundle.getPressAction().toBundle());
              } else {
                // this method too
                intents =
                  ReceiverService.createNormalIntent(
                    ACTION_PRESS_INTENT,
                    new String[] {"notification", "pressAction"},
                    notificationModel.toBundle(),
                    actionBundle.getPressAction().toBundle());
              }
              shortcutInfoBuilder.setIntents(intents);
            }

            if (shortcutInfoBundle.containsKey("persons")) {
              List<Person> persons = new ArrayList<>();
              ArrayList<Bundle> personBundles = shortcutInfoBundle.getParcelableArrayList("persons");
              for (Bundle personBundle : personBundles) {
                Person person = Tasks.await(NotificationAndroidStyleModel.getPerson(CACHED_THREAD_POOL, personBundle), 10, TimeUnit.SECONDS);
                if (person != null) {
                  persons.add(person);
                }
              }
              shortcutInfoBuilder.setPersons(persons.toArray(new Person[0]));
            }

            ShortcutInfoCompat shortcutInfo = shortcutInfoBuilder.build();

            ShortcutManagerCompat.pushDynamicShortcut(getApplicationContext(), shortcutInfo);

            builder.setShortcutInfo(shortcutInfo);
          }

          return builder;
        };
kdwkr commented 1 month ago

test payload (NotificationAndroid)

{
    channelId: "default",
    smallIcon: 'ic_notification',
    showTimestamp: true,
    category: 'msg',
    color: '#000000',
    shortcutInfo: {
        id: 'channel-id',
        shortLabel: 'name-of-person',
        icon: 'https://example.com/image.png',
        action: {
            title: 'Chat',
            pressAction: {
                id: 'navigate-channel-id'
            } 
        },
        persons: [{
            id: 'user-id',
            name: 'name-of-person',
            icon: 'https://example.com/image.png',
        }],
        longLived: true,
    },
    actions: [
      {
        title: 'Chat',
        pressAction: {
          id: 'reply-channel-id'
        },
        input: {
          choices: ['Hi', 'Hello'],
          placeholder: 'Chat with a person',
        },
      },
    ],
    style: {
        type: 3,
        person: {
            name: 'Me',
            icon: 'My Profile Image',
        },
        messages: [{
            text: 'Hi?',
            timestamp: Date.now(),
            person: {
                id: 'user-id',
                name: 'Name of person',
                icon: 'https://example.com/image.png',
            },
        }]
    }
}
github-actions[bot] commented 3 weeks ago

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

kdwkr commented 3 weeks ago

It will be helpful. Please consider this

eman747 commented 5 days ago

how can we show messaging style with large icon