question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Group notifications

See original GitHub issue

Question

Hello! How does group and groupSummary work? I recently migrated from React Native Firebase V5, to this library to handle local notifications. In RNFirebase I had to create and display 2 notifications, one that should be only for summary group and and another to handle the notification itself and asign it to the group.

In this library, the option groupSummary is present, but it isn’t seem to be working for me, or maybe I’m doing it wrong. I’m creating/displaying the notification with the code below. I want to group notifications in separated groups. Should I use two notifications, like on RNFirebase? If so, in what format should they be?

Another thing that I realized is that there is not groupAlertBehavior option, to handle a notification that will be only for the group summary.

Thanks!

PushNotification.localNotification({
            /* Android Only Properties */
            autoCancel: true, // (optional) default: true
            largeIcon: "", // (optional) default: "ic_launcher". Use "" for no large icon.
            smallIcon: "ic_notification", // (optional) default: "ic_notification" with fallback for "ic_launcher". Use "" for default small icon.
            vibrate: true,
            vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
            tag: "approved", // (optional) add tag to message
            group: "approved", // (optional) add group to message
            groupSummary: true, // (optional) set this notification to be the group summary for a group of notifications, default: false
            priority: "max", // (optional) set notification priority, default: high
            importance: "max", // (optional) set notification importance, default: high
            channelId: DEFAULT_CHANNEL_ID, // (optional) custom channelId, if the channel doesn't exist, it will be created with options passed above (importance, vibration, sound). Once the channel is created, the channel will not be update. Make sure your channelId is different if you change these options. If you have created a custom channel, it will apply options of the channel.

            /* iOS and Android properties */
            title: message.data.title, // (optional)
            message: message.data.body, // (required)
            playSound: true, // (optional) default: true
            soundName: "default", // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
            number: message.data.badge, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero)
});

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

7reactions
OmarBasemcommented, Aug 27, 2020

@FranciscoCHT I have found a better solution without the need of extra channels. Using the following code you should not need an extra channel:

// POP UP NOTIFICATION
        PushNotification.localNotification({
            data: notification,
            showWhen: true, // (optional) default: true
            autoCancel: true, // (optional) default: true
            smallIcon: '@drawable/ic_stat_name', // (optional) default: "ic_notification" with fallback for "ic_launcher". Use "" for default small icon.
            subText: notification.subText, // (optional) default: none
            color: '#8000ff', // (optional) default: system default
            vibrate: true, // (optional) default: true
            vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
            group: notification.channel_id, // (optional) add group to message
            ongoing: false, // (optional) set whether this is an "ongoing" notification
            priority: "high", // (optional) set notification priority, default: high
            visibility: "private", // (optional) set notification visibility, default: private
            importance: "high", // (optional) set notification importance, default: high
            allowWhileIdle: false, // (optional) set notification to work while on doze, default: false
            ignoreInForeground: false, // (optional) if true, the notification will not be visible when the app is in the foreground (useful for parity with how iOS notifications appear)
            channelId: notification.channel_id, // (optional) custom channelId, if the channel doesn't exist, it will be created with options passed above (importance, vibration, sound). Once the channel is created, the channel will not be update. Make sure your channelId is different if you change these options. If you have created a custom channel, it will apply options of the channel.
            onlyAlertOnce: false, //(optional) alert will open only once with sound and notify, default: false
            messageId,
            invokeApp: true, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true
            alertAction: "view", // (optional) default: view
            category: "", // (optional) default: empty string
            title: notification.title, // (optional)
            message: notification.body, // (required)
            userInfo: {}, // (optional) default: {} (using null throws a JSON value '<null>' error)
            playSound: true, // (optional) default: true
            soundName: "default", // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played)
        });

// GROUP SUMMARY 
        PushNotification.localNotification({
            channelId: notification.channel_id,
            id: this.ids[notification.channel_id],
            group: notification.channel_id,
            subText: notification.subText,
            smallIcon: '@drawable/ic_stat_name',
            color: '#8000ff',
            groupSummary: true,
             title: notification.title, 
            message: notification.body,
            data: notification,

        })
7reactions
FranciscoCHTcommented, Aug 21, 2020

Ended up following the same logic as I did with RNFirebase V5 (Android only).

I created two PushNotification.localNotification, one for the notification itself, and another for creating the groupSummary that will group the others notifications.

The problem with this was that onNotification it will show 2 popup notifications at the same time (one grouped and the one arriving). In Android, this is fixed by adding the parameter groupAlertBehavior which allows to set to silent the groupSummary notification, but it hasn’t been added to this library yet.

So, since I hadn’t this parameter, and wanted only to show the arriving notification as popup, and the group notification silent, I had to create a special channel, with PushNotification.createChannel, and configured it to be silent, then assigned it to the groupSummary notification.

PushNotification.createChannel({
            channelId: POPUP_CHANNEL_ID,
            channelName: POPUP_CHANNEL_NAME,
            channelDescription: POPUP_CHANNEL_DESC,
            soundName: "default",
            importance: 5,     // <- POPUP NOTIFICATION CHANNEL
            vibrate: true,
});
PushNotification.createChannel({
            channelId: SILENT_CHANNEL_ID,
            channelName: SILENT_CHANNEL_NAME,
            channelDescription: SILENT_CHANNEL_DESC,
            importance: 2,     // <- SILENT NOTIFICATION CHANNEL
            vibrate: false,
});

Then assigned it to the 2 local notifications. The POPUP CHANNEL to the arriving notification, and the SILENT CHANNEL to the groupSummary notification.

/* POPUP NOTIFICATION */
PushNotification.localNotification({
            /* Android Only Properties */
            autoCancel: true,
            smallIcon: "ic_notification",
            vibrate: true,
            group: message.data.groupName,
            channelId: POPUP_CHANNEL_ID,  // <- ASSIGN POPUP CHANNEL CREATED

            /* iOS and Android properties */
            title: message.data.title,
            message: message.data.body,
            playSound: true,
            soundName: "default",
            number: message.data.badge,
});

/* GROUP SUMMARY NOTIFICATION */
PushNotification.localNotification({
            /* Android Only Properties */
            autoCancel: true, 
            smallIcon: "ic_notification",
            group: message.data.groupName, 
            groupSummary: true,          // <- REQUIRED IN GROUP SUMMARY NOTIFICATION
            channelId: SILENT_CHANNEL_ID, // <- ASSIGN SILENT CHANNEL CREATED

            /* iOS and Android properties */
            id: groupId[message.data.groupName], // REQUIRED, HAS TO BE A NUMBER
            message: "", //REQUIRED
});

The group summary notification has to have the same ID in each group you want to create, otherwise it will group in different notifications, and has to be a number, otherwise it’ll not work.

This should group notifications that share the same group parameter, and mantain silent the group notifications. I know that the creation of another channel isn’t ideal, and I don’t know if there is a simpler solution, but feel free to share any finds! 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use notifications on your iPhone or iPad - Apple Support
Go to Settings and tap Notifications. · Select an app and tap Notification Grouping. · Select one of these options: Automatic: The notifications...
Read more >
Create a Group of Notifications - Android Developers
Grouped notifications must have an extra notification which acts as the group summary. To enable grouped notifications, you must set a group ......
Read more >
How to Group Notifications By App on iPhone or iPad
How to Group Notifications By App on iPhone or iPad. Step #1. Open Settings app on your iOS device → Now, tap on...
Read more >
Notification Grouping - OneSignal Documentation
Automatically groups your notifications after the device has 4 or more visible notifications for your app, even if you don't set a group...
Read more >
How to Customize Grouped Notifications in iOS 12 - MacRumors
1. Open up the Settings app. 2. Choose "Notifications" from the main list of options. 3. Find the app with the notifications you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found