Last active
September 5, 2018 08:19
-
-
Save sugumura/f9bf1a18768fc5275828aca76a6e9934 to your computer and use it in GitHub Desktop.
IonicでのFirebaseによるプッシュ通知用のトークンを取得するサンプル。
This file contains hidden or 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
// src/app/app.module.ts | |
import { Firebase } from '@ionic-native/firebase'; | |
@NgModule({ | |
//... | |
providers: [ | |
Firebase, | |
], | |
//... | |
}) | |
export class AppModule {} |
This file contains hidden or 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
// src/pages/sample/sample.ts | |
// import省略 | |
@IonicPage() | |
@Component({ | |
selector: 'page-sample', | |
templateUrl: 'sample.html', | |
}) | |
export class SamplePage { | |
constructor(public platform: Platform, private firebase: Firebase,) { | |
this.platform.ready().then(() => { | |
if (this.platform.is('ios')) { | |
this.getPushTokenIOS() | |
.pipe( | |
mergeMap(token => this.requestDevice(token)) | |
).subscribe({ | |
next: response => { | |
// newsトピックを購読する | |
this.firebase.subscribe('news') | |
}, | |
error: error => { | |
console.error(error); | |
} | |
}); | |
} | |
if (this.platform.is('android')) { | |
this.getPushTokenAndroid() | |
.pipe( | |
mergeMap(token => this.requestDevice(token)) | |
).subscribe({ | |
next: response => { | |
// newsトピックを購読する | |
this.firebase.subscribe('news') | |
}, | |
error: error => { | |
console.error(error); | |
} | |
}); | |
} | |
}) | |
} | |
getPushTokenIOS(): Observable<string|boolean> { | |
return this.hasPushPermission() | |
.pipe( | |
mergeMap((permission) => { | |
if (permission.isEnabled) { | |
return this.getPushToken(); | |
} else { | |
return this.grantPushPermission() | |
.pipe( | |
mergeMap((grant) => { | |
return this.getPushToken(); | |
}), | |
catchError(error => { | |
return of(false); | |
}) | |
); | |
} | |
}), | |
) | |
} | |
getPushTokenAndroid(): Observable<string|boolean> { | |
return this.hasPushPermission() | |
.pipe( | |
mergeMap(permission => { | |
if (permission.isEnabled) { | |
return this.getPushToken(); | |
} else { | |
return of(false); | |
} | |
}) | |
) | |
} | |
private requestDevice(token: string): Observable<any> { | |
if (typeof token === 'string') { | |
// デバイストークンをサーバに保存する | |
// return Observable<any> | |
} else { | |
return _throw('push token get error'); | |
} | |
} | |
private hasPushPermission(): Observable<{ isEnabled: boolean; }> { | |
return fromPromise(this.firebase.hasPermission()); | |
} | |
private grantPushPermission(): Observable<any> { | |
return fromPromise(this.firebase.grantPermission()); | |
} | |
private getPushToken(): Observable<any> { | |
return fromPromise(this.firebase.getToken()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment