-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Support InboxStyle of Android notifications. #1323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@Salakar this is an example: // use constant ID for notification used as group summary
const SUMMARY_ID = '12345';
const CHANNEL_ID = 'com.android.example.channel';
const GROUP_KEY = 'com.android.example.group';
const STATUS_ICON = 'ic_notification';
const newNotification1 = new firebase.notifications.Notification()
.setTitle('Justin Rhyss')
.setBody('You will not believe...')
.android.setChannelId(CHANNEL_ID)
.android.setSmallIcon(STATUS_ICON)
.android.setGroup(GROUP_KEY);
const newNotification2 = new firebase.notifications.Notification()
.setTitle('Ali Connors')
.setBody('Please join us to celebrate the...')
.android.setChannelId(CHANNEL_ID)
.android.setSmallIcon(STATUS_ICON)
.android.setGroup(GROUP_KEY);
const summaryNotification = new firebase.notifications.Notification()
.setNotificationId(SUMMARY_ID)
.setTitle('Email Summary')
// set content text to support devices running API level < 24
.setBody('Two new messages')
.setSound('default')
.android.setChannelId(CHANNEL_ID)
.android.setSmallIcon(STATUS_ICON)
// build summary info into InboxStyle template
.android.setInboxStyle(
[],
'2 new messages',
'[email protected]'
)
// specify which group this notification belongs to
.android.setGroup(GROUP_KEY)
// set this notification as the summary for the group
.android.setGroupSummary(true);
firebase.notifications().displayNotification(newNotification1);
firebase.notifications().displayNotification(newNotification2);
firebase.notifications().displayNotification(summaryNotification); The summary notification ID should stay the same so that it is only posted once, and so you can update it later if the summary information changes (subsequent additions to the group should result in updating the existing summary). |
@aMarCruz this is a great start to adding support for this 🎉 thank you! Given that there are multiple const SUMMARY_ID = '12345';
const STATUS_ICON = 'ic_notification';
const GROUP_KEY = 'com.android.example.group';
const CHANNEL_ID = 'com.android.example.channel';
const notification = new firebase.notifications.Notification();
const inboxStyle = new firebase.notifications.AndroidNotificationStyleInbox();
notification.setSound('default');
notification.setTitle('Email Summary');
notification.setBody('Two new messages');
notification.setNotificationId(SUMMARY_ID);
// inboxStyle.addLine('foo'); // lines.push()
// inboxStyle.addLine('barb'); // lines.push();
inboxStyle.setTitle('2 new messages'); // contentTitle
inboxStyle.setBody('[email protected]'); // summaryText
// Android specific
notification.android.setGroup(GROUP_KEY);
notification.android.setStyle(inboxStyle);
notification.android.setGroupSummary(true);
notification.android.setChannelId(CHANNEL_ID);
notification.android.setSmallIcon(STATUS_ICON); When it serializes to send to/from native it could just add an extra property of const style = {
style: 'inbox',
lines: [],
title: '2 new messages', // contentTitle
body: '[email protected]', // summaryText
}; Thoughts? Loving
|
@Salakar I agree.
The rest (Messaging, Media, DecoratedCustom, DecoratedMediaCustomView) seem a bit more complex and could be implemented gradually in future versions, as needed. |
@Salakar, On the other hand, the use of classes would be very similar to the Android API, which can help RNFB make a better check and can be an advantage for those who are related to Android. I don't know, but if only these 3 are implemented, including that of this PR, it could be handled by TS or Flow typings, as follows: // Typescript
interface BaseStyle {
style: 'bigPicture' | 'bigText' | 'inbox';
title?: string;
body?: string;
}
interface BigPicture extends BaseStyle {
style: 'bigPicture';
picture: string;
largeIcon?: string;
}
interface BigText extends BaseStyle {
style: 'bigText';
text: string;
}
interface Inbox extends BaseStyle {
style: 'inbox';
lines: string[];
}
type NotificationStyle = BigPicture | BigText | Inbox;
//. . .
setStyle(style: NotificationStyle): Notification; usage: const notification = new firebase.notifications.Notification()
.setStyle({
style: 'bigText',
title: 'BigText Sample',
picture: 'mypicture.png',
}) |
@aMarCruz thanks for your feedback and suggestions. I agree also that I think that as it is an Android-only thing it's good that it's similar to how the Android API is, it's also similar to the rest of the notifications library and other modules in RNFB that use a builder approach anyway, e.g. Dynamic Links uses a Additionally, having it class based also allows future proofing for when we handle the more complex ones that may require asynchronous calls over the react native bridge, e.g. You could have a There should also probably be a default style if none provided as well, I guess |
@Salakar ,
Yes, of course.
I'm not sure I understand this (English is not my language). The plubic API, based on classes and close to the Android API, could look like this: // Typescript - Android namespace
declare abstract class BaseStyle {
readonly summary?: string | void;
readonly title: string | void;
}
declare class BigPictureStyle extends BaseStyle {
readonly picture: string;
readonly largeIcon: string | void;
setTitle(title: string): this;
setSummary(body: string): this;
setPicture(picture: String): this; // required?
setLargeIcon(icon: string): this;
}
declare class BigTextStyle extends BaseStyle {
readonly text: string;
setTitle(title: string): this;
setSummary(body: string): this;
setText(text: String): this; // required
}
declare class InboxStyle extends BaseStyle {
readonly lines: string[]; // there's no getLines() in Android InboxStyle
setTitle(title: string): this;
setSummary(body: string): this;
addLine(line: String): this;
}
// As example, the MediaStyle is simple and RNFB can expose it, but
// an external library must provide the MediaSession.Token object.
declare class MediaStyle extends BaseStyle {
setMediaSession(token: Object): this;
setShowActionsInCompactView(...actions: number[]): this;
}
// MessagingStyle is complex and require additional classes
declare class Person {
// ...
}
declare class Message {
constructor(text: string, timestamp: Date, sender?: Person)
// ...
}
declare class MessagingStyle extends BaseStyle {
readonly historicMessages: Message[];
readonly messages: Message[];
readonly user: Person;
readonly userDisplayName: string;
readonly groupConversation: boolean;
constructor(user: Person);
setTitle(title: string): this;
setGroupConversation(group: boolean): this;
addMessage(text: String, timestamp?: Date, sender?: Person): this;
addMessage(message: Message): this;
addHistoricMessage(message): this;
static Message: typeof Message;
}
// DecoratedMediaCustomViewStyle requires some methods not yet implemented in AndroidNotification.
// ...
setStyle(style: BaseStyle): Notification; with your feedback, in the next few days I can submit a new PR with the implementation of the first 3 or 4 styles and MessagingStyle, if required, in a later PR. |
This issue has been automatically marked as stale because it has not had recent user activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@AlcuinGuest this will be introduced in version 6 once we get around to notifications. |
Hi, any update on MessagingStyle .? |
This commit adds suport for the InboxStyle of Android notifications.
An overview is in the guides Create a Group of Notifications and Create an Expandable Notification of the Google Developers docs.
The commit includes Flow and Typescript definitions, but no test nor documentation.
The InboxStyle is supported from API level 16 and NotificationCompat 22.1.0.