Skip to content

Instantly share code, notes, and snippets.

View nomadts's full-sized avatar

Arman Murzabulatov nomadts

View GitHub Profile
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
}
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() {
// 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)) {
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
console.log('Notification permission granted.');
} else {
console.log('Unable to get permission to notify.');
}
});
new Notification('Your title here', { body: 'Your message here' });
if (!('Notification' in window)) {
console.log('This browser does not support desktop notification');
}
Notification.requestPermission().then(function(permission) {
if (permission === 'granted') {
new Notification('Hello! This is a notification.');
}
});
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>
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class GeolocationService {
constructor() { }
getCurrentPosition(): Observable<GeolocationPosition> {
return new Observable((observer) => {
/** Geolocation API **/
navigator.geolocation;