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
import { isPlatformBrowser } from '@angular/common'; | |
import { PLATFORM_ID } from '@angular/core'; | |
// ... | |
constructor(@Inject(PLATFORM_ID) private platformId: object) {} | |
if (isPlatformBrowser(this.platformId)) { | |
// Browser-specific code | |
} |
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
import { Component } from '@angular/core'; | |
import { NotificationService } from './path-to-notification.service'; | |
@Component({ | |
selector: 'app-your-component', | |
templateUrl: './your-component.component.html', | |
}) | |
export class YourComponent { | |
constructor(private notificationService: NotificationService) {} | |
someMethod() { |
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
// notification.service.ts | |
import { Injectable } from '@angular/core'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class NotificationService { | |
constructor() { } | |
public notify(title: string, options?: NotificationOptions): void { | |
if (!('Notification' in window)) { |
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
Notification.requestPermission().then(permission => { | |
if (permission === 'granted') { | |
console.log('Notification permission granted.'); | |
} else { | |
console.log('Unable to get permission to notify.'); | |
} | |
}); |
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
new Notification('Your title here', { body: 'Your message here' }); |
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
if (!('Notification' in window)) { | |
console.log('This browser does not support desktop notification'); | |
} |
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
Notification.requestPermission().then(function(permission) { | |
if (permission === 'granted') { | |
new Notification('Hello! This is a notification.'); | |
} | |
}); |
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
import { Component, OnInit } from '@angular/core'; | |
import { GeolocationService } from './geolocation.service'; | |
@Component({ | |
selector: 'app-location', | |
template: ` | |
<div> | |
<p>Latitude: {{ latitude }}</p> | |
<p>Longitude: {{ longitude }}</p> | |
</div> |
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
import { Injectable } from '@angular/core'; | |
import { Observable } from 'rxjs'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class GeolocationService { | |
constructor() { } | |
getCurrentPosition(): Observable<GeolocationPosition> { | |
return new Observable((observer) => { |
NewerOlder