-
-
Notifications
You must be signed in to change notification settings - Fork 823
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show badge counter on web favicon, desktop dock and desktop tray
Related: #90
- Loading branch information
1 parent
d84fd17
commit f1c600a
Showing
24 changed files
with
150 additions
and
28 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 47 additions & 7 deletions
54
packages/components/src/components/common/AppIconBadge.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,54 @@ | ||
import React from 'react' | ||
|
||
import { Platform } from '../../libs/platform' | ||
export interface AppIconBadgeProps {} | ||
|
||
export const AppIconBadge = React.memo((_props: AppIconBadgeProps) => { | ||
return null | ||
}) | ||
|
||
// Before enabling this for mobile, needs to: | ||
// 1. Fix badge on Android | ||
// 2. Make sure the badge number will get automatically updated on background | ||
/* | ||
import React from 'react' | ||
import firebase from 'react-native-firebase' | ||
import { useUnreadCount } from '../context/UnreadCountContext' | ||
interface IProps {} | ||
export interface AppIconBadgeProps {} | ||
export const AppIconBadge = React.memo((_props: AppIconBadgeProps) => { | ||
const unreadCount = useUnreadCount() | ||
askPermissionAndSetBadge(unreadCount) | ||
export const AppIconBadge = React.memo((_props: IProps) => { | ||
if (Platform.isElectron) { | ||
const unreadCount = useUnreadCount() | ||
window.ipc.send('unread-counter', unreadCount) | ||
} | ||
return null | ||
}) | ||
async function askPermissionAndSetBadge(badge: number) { | ||
let hasPermission | ||
try { | ||
hasPermission = await firebase.messaging().hasPermission() | ||
} catch (error) { | ||
if (__DEV__) alert(`Failed to check notifications permission. ${error}`) | ||
} | ||
if (!hasPermission) { | ||
try { | ||
await firebase.messaging().requestPermission() | ||
hasPermission = await firebase.messaging().hasPermission() | ||
} catch (error) { | ||
if (__DEV__) alert(`Failed to get notifications permission. ${error}`) | ||
} | ||
} | ||
if (!hasPermission) { | ||
if (__DEV__) alert('Failed to get permission to set the badge.') | ||
return | ||
} | ||
firebase.notifications().setBadge(badge) | ||
return hasPermission | ||
} | ||
*/ |
49 changes: 49 additions & 0 deletions
49
packages/components/src/components/common/AppIconBadge.web.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React from 'react' | ||
|
||
import { Platform } from '../../libs/platform' | ||
import { useUnreadCount } from '../context/UnreadCountContext' | ||
|
||
export interface AppIconBadgeProps {} | ||
|
||
export const AppIconBadge = React.memo((_props: AppIconBadgeProps) => { | ||
const unreadCount = useUnreadCount() | ||
|
||
if (Platform.isElectron) { | ||
window.ipc.send('unread-counter', unreadCount) | ||
} else { | ||
const title = `${document.title || 'DevHub'}` | ||
const titleWithoutBadge = title.replace(/ \([\d]+\+?\)$/, '') | ||
|
||
document.title = `${titleWithoutBadge}${ | ||
unreadCount > 0 ? ` (${unreadCount})` : '' | ||
}` | ||
updateFavicon(unreadCount > 0) | ||
} | ||
|
||
return null | ||
}) | ||
|
||
function updateFavicon(showBadge: boolean) { | ||
let favicon = document.querySelector('link[rel="shortcut icon"]') | ||
|
||
if (!favicon) { | ||
const head = document.querySelector('head') | ||
if (!head) return false | ||
|
||
favicon = document.createElement('link') | ||
favicon.setAttribute('rel', 'shortcut icon') | ||
|
||
head.appendChild(favicon) | ||
} | ||
|
||
const href = favicon.getAttribute('href') || 'favicon.ico' | ||
|
||
const baseArr = href.split('/').slice(0, -1) | ||
const newFilename = showBadge ? 'favicon-with-badge.ico' : 'favicon.ico' | ||
const newHref = [...baseArr, newFilename].join('/') | ||
|
||
favicon.setAttribute('type', 'image/ico') | ||
favicon.setAttribute('href', newHref) | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.