import { Injectable , Optional , ComponentFactoryResolver , ApplicationRef , Injector , EmbeddedViewRef , EventEmitter , } from '@angular/core' ; import { Observable } from 'rxjs' ; import { Permission , PushNotify } from './interfaces/push.type' ; declare const Notification ; @ Injectable ( ) export class NotifyPushService { permission : Permission ; constructor ( ) { this . permission = this . isSupported ( ) ? Notification . permission : 'denied' ; } requestPermission ( ) { if ( this . isSupported ( ) ) Notification . requestPermission ( ( status ) => ( this . permission = status ) ) ; } isSupported ( ) { return 'Notification' in window ; } create ( title , options ? ) { return new Observable ( ( obs ) => { if ( ! this . isSupported ( ) ) { obs . error ( '不支持' ) ; obs . complete ( ) ; } if ( this . permission !== 'granted' ) { obs . error ( `用户未授权` ) ; obs . complete ( ) ; } const n = new Notification ( title , options ) ; n . onshow = ( e ) => obs . next ( { notification : n , event : e } ) ; n . onclick = ( e ) => obs . next ( { notification : n , event : e } ) ; n . onerror = ( e ) => obs . error ( { notification : n , event : e } ) ; n . onclose = ( ) => obs . complete ( ) ; } ) ; } }