@@ -45,6 +45,13 @@ impl Cmd {
4545 context : InvokeContext < R > ,
4646 options : NotificationOptions ,
4747 ) -> crate :: Result < ( ) > {
48+ let allowed = match is_permission_granted ( & context) {
49+ Some ( p) => p,
50+ None => request_permission ( & context) ,
51+ } ;
52+ if !allowed {
53+ return Err ( crate :: Error :: NotificationNotAllowed ) ;
54+ }
4855 let mut notification =
4956 Notification :: new ( context. config . tauri . bundle . identifier . clone ( ) ) . title ( options. title ) ;
5057 if let Some ( body) = options. body {
@@ -61,39 +68,7 @@ impl Cmd {
6168 fn request_notification_permission < R : Runtime > (
6269 context : InvokeContext < R > ,
6370 ) -> crate :: Result < & ' static str > {
64- let mut settings = crate :: settings:: read_settings (
65- & context. config ,
66- & context. package_info ,
67- context. window . state :: < Env > ( ) . inner ( ) ,
68- ) ;
69- if let Some ( allow_notification) = settings. allow_notification {
70- return Ok ( if allow_notification {
71- PERMISSION_GRANTED
72- } else {
73- PERMISSION_DENIED
74- } ) ;
75- }
76- let ( tx, rx) = std:: sync:: mpsc:: channel ( ) ;
77- crate :: api:: dialog:: ask (
78- Some ( & context. window ) ,
79- "Permissions" ,
80- "This app wants to show notifications. Do you allow?" ,
81- move |answer| {
82- tx. send ( answer) . unwrap ( ) ;
83- } ,
84- ) ;
85-
86- let answer = rx. recv ( ) . unwrap ( ) ;
87-
88- settings. allow_notification = Some ( answer) ;
89- crate :: settings:: write_settings (
90- & context. config ,
91- & context. package_info ,
92- context. window . state :: < Env > ( ) . inner ( ) ,
93- settings,
94- ) ?;
95-
96- if answer {
71+ if request_permission ( & context) {
9772 Ok ( PERMISSION_GRANTED )
9873 } else {
9974 Ok ( PERMISSION_DENIED )
@@ -111,12 +86,7 @@ impl Cmd {
11186 fn is_notification_permission_granted < R : Runtime > (
11287 context : InvokeContext < R > ,
11388 ) -> crate :: Result < Option < bool > > {
114- let settings = crate :: settings:: read_settings (
115- & context. config ,
116- & context. package_info ,
117- context. window . state :: < Env > ( ) . inner ( ) ,
118- ) ;
119- if let Some ( allow_notification) = settings. allow_notification {
89+ if let Some ( allow_notification) = is_permission_granted ( & context) {
12090 Ok ( Some ( allow_notification) )
12191 } else {
12292 Ok ( None )
@@ -130,3 +100,46 @@ impl Cmd {
130100 Ok ( Some ( false ) )
131101 }
132102}
103+
104+ #[ cfg( notification_all) ]
105+ fn request_permission < R : Runtime > ( context : & InvokeContext < R > ) -> bool {
106+ let mut settings = crate :: settings:: read_settings (
107+ & context. config ,
108+ & context. package_info ,
109+ context. window . state :: < Env > ( ) . inner ( ) ,
110+ ) ;
111+ if let Some ( allow_notification) = settings. allow_notification {
112+ return allow_notification;
113+ }
114+ let ( tx, rx) = std:: sync:: mpsc:: channel ( ) ;
115+ crate :: api:: dialog:: ask (
116+ Some ( & context. window ) ,
117+ "Permissions" ,
118+ "This app wants to show notifications. Do you allow?" ,
119+ move |answer| {
120+ tx. send ( answer) . unwrap ( ) ;
121+ } ,
122+ ) ;
123+
124+ let answer = rx. recv ( ) . unwrap ( ) ;
125+
126+ settings. allow_notification = Some ( answer) ;
127+ let _ = crate :: settings:: write_settings (
128+ & context. config ,
129+ & context. package_info ,
130+ context. window . state :: < Env > ( ) . inner ( ) ,
131+ settings,
132+ ) ;
133+
134+ answer
135+ }
136+
137+ #[ cfg( notification_all) ]
138+ fn is_permission_granted < R : Runtime > ( context : & InvokeContext < R > ) -> Option < bool > {
139+ crate :: settings:: read_settings (
140+ & context. config ,
141+ & context. package_info ,
142+ context. window . state :: < Env > ( ) . inner ( ) ,
143+ )
144+ . allow_notification
145+ }
0 commit comments