-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from PokemonGoers/notifications
PokeMob notifications
- Loading branch information
Showing
8 changed files
with
143 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
import { Component } from '@angular/core'; | ||
import { Platform } from 'ionic-angular'; | ||
import { Component, ViewChild } from '@angular/core'; | ||
import { Platform, Nav } from 'ionic-angular'; | ||
import { StatusBar, Splashscreen } from 'ionic-native'; | ||
import 'rxjs/add/operator/map'; | ||
|
||
import { MapPage } from '../pages/map/map.page'; | ||
import { NotificationService } from '../services/notification.service'; | ||
|
||
@Component({ | ||
templateUrl: './app.html' | ||
}) | ||
export class App { | ||
rootPage = MapPage; | ||
|
||
constructor(private platform: Platform) { | ||
@ViewChild('content') nav: Nav; | ||
|
||
constructor(private platform: Platform, private notificationService: NotificationService) { | ||
platform.ready().then(() => { | ||
// Okay, so the platform is ready and our plugins are available. | ||
// Here you can do any higher level native things you might need. | ||
StatusBar.styleDefault(); | ||
Splashscreen.hide(); | ||
if (platform.is('cordova')) { | ||
StatusBar.styleDefault(); | ||
Splashscreen.hide(); | ||
} | ||
|
||
if (platform.is('android') && !platform.is('mobileweb')) { | ||
notificationService.setNav(this.nav); | ||
notificationService.scheduleNotifications(); | ||
} | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Nav } from 'ionic-angular'; | ||
import { LocalNotifications } from 'ionic-native'; | ||
|
||
import { Mob } from '../models/mob'; | ||
import { MapPage } from '../pages/map/map.page'; | ||
import { ApiService } from './api.service'; | ||
|
||
@Injectable() | ||
export class NotificationService { | ||
|
||
private MOB_NOTIFICATION_ID = 1; | ||
private MOB_ZOOM_LEVEL = 15; | ||
|
||
private nav: Nav; | ||
|
||
constructor(private apiService: ApiService) {} | ||
|
||
setNav(nav: Nav) { | ||
this.nav = nav; | ||
} | ||
|
||
scheduleNotifications() { | ||
this.apiService.subscribeToMobs(this.showMobNotification.bind(this)); | ||
|
||
LocalNotifications.on('click', notification => { | ||
switch(notification.id) { | ||
case this.MOB_NOTIFICATION_ID: | ||
this.onMobNotificationClick(JSON.parse(notification.data)); | ||
break; | ||
} | ||
}); | ||
} | ||
|
||
showMobNotification(mob: Mob) { | ||
// https://github.com/katzer/cordova-plugin-local-notifications/wiki/04.-Scheduling | ||
LocalNotifications.schedule({ | ||
id: this.MOB_NOTIFICATION_ID, | ||
title: 'PredictEmAll - PokeMob detected', | ||
text: mob.tweets.length + ' tweets about Pokémon were posted in your area', | ||
icon: 'res://icon.png', | ||
smallIcon: 'res://icon.png', | ||
data: mob | ||
}); | ||
} | ||
|
||
onMobNotificationClick(mob: Mob) { | ||
let parameters: any = {}; | ||
parameters.position = { | ||
coordinates: { | ||
latitude: mob.coordinates[1], | ||
longitude: mob.coordinates[0] | ||
}, | ||
zoomLevel: this.MOB_ZOOM_LEVEL | ||
}; | ||
|
||
this.nav.setRoot(MapPage, parameters); | ||
} | ||
|
||
} |
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,30 @@ | ||
import * as io from 'socket.io-client'; | ||
import { Injectable } from '@angular/core'; | ||
import { ConfigService } from './config.service'; | ||
|
||
@Injectable() | ||
export class WebsocketService { | ||
|
||
private connected: Promise<any>; | ||
|
||
constructor(config: ConfigService) { | ||
let socket: any = io.connect(config.websocketEndpoint); | ||
|
||
this.connected = new Promise((resolve, reject) => { | ||
socket.on('connect', () => resolve(socket)); | ||
}); | ||
} | ||
|
||
on(key: string, callback: ((any) => any)) { | ||
this.connected.then(socket => { | ||
socket.on(key, callback); | ||
}); | ||
} | ||
|
||
emit(key: string, data: any) { | ||
this.connected.then(socket => { | ||
socket.emit(key, data); | ||
}) | ||
} | ||
|
||
} |