2017-03-22 8 views
1

私は角度2とFirebaseにはかなり新しいですが、チャットアプリhereを作成できました。私の最終目標はチャットアプリに新しいメッセージがあるときにプッシュ通知を作成することです。チャットを受け取ったときにプッシュ通知を作成する

  1. 私はthisをインストールしようとしたが、私は、ドキュメントを理解できなかったとして、それは非常に困難です。

  2. this threadから解決策を試しました。アプリは通知を検出しません。

この権利にアプローチする方法についてのヘルプはありますか?

私はあなたのコンポーネントでメッセージデータベースに加入するだろう、これは私のapp.component.ts

import { Component } from '@angular/core'; 
import { AngularFire, AuthProviders, AuthMethods, FirebaseListObservable } from 'angularfire2'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 

export class AppComponent { 
    items: FirebaseListObservable<any>; 
    name: any; 
    msgVal: string = ''; 

    constructor(public af: AngularFire) { 
     this.items = af.database.list('/messages', { 
      query: { 
      limitToLast: 5 
      } 
     }); 

     this.af.auth.subscribe(auth => { 
      if(auth) { 
      this.name = auth; 
      } 
     }); 
    } 

login() { 
    this.af.auth.login({ 
     provider: AuthProviders.Facebook, 
     method: AuthMethods.Popup, 
    }); 
    } 

chatSend(theirMessage: string) { 
     this.items.push({ message: theirMessage, name: this.name.facebook.displayName}); 
     this.msgVal = ''; 
    } 

mychange(){ 
    console.log("change happned"); // updated value 
    } 
} 

で、私のapp.moduleファイルが含まれている、

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 
import { AppComponent } from './app.component'; 
import { AngularFireModule} from 'angularfire2'; 
import {initializeApp,database} from 'firebase'; 

export const firebaseConfig = { 
    apiKey: "AIzaSyAQUetWt-pH-WuMTZ-lonP2ykICOl4KiPw", 
    authDomain: "anguchat-eb370.firebaseapp.com", 
    databaseURL: "https://anguchat-eb370.firebaseio.com", 
    storageBucket: "anguchat-eb370.appspot.com", 
    messagingSenderId: "267732203493" 
}; 

initializeApp(firebaseConfig); 
database().ref().on('value', function(snapshot){ 
    var x = snapshot.val() 
    console.log("This prints whenever there is a new message"); 
}); 

@NgModule({ 
    declarations: [AppComponent], 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule, 
    AngularFireModule.initializeApp(firebaseConfig) 
    ], 
    providers: [], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 
+0

あなたは別のデバイス*へ*デバイス*から*直接Firebaseクラウドメッセージングとメッセージを送信することはできません。 http://stackoverflow.com/q/37435750、http://stackoverflow.com/q/37990140、およびhttp://stackoverflow.com/q/38372147を参照してください。ただし、Firebaseでクラウド機能を使用して安全に送信することはできます通知。 https://firebase.google.com/docs/functions/use-cases#notify_users_when_something_interesting_happensを参照してください –

+0

申し訳ありませんが、私はそれを正しく説明できませんでした。 私はブラウザの通知を取得しようとしています。 これらのhttp:// www。 girliemac.com/assets/images/articles/2014/03/notifications.png – Pujan

+0

はい、私はそれを持っています。そして、[FCM.jsはそれらを受け取ることができます](https://firebase.google.com/docs/cloud-messaging/js/client)。しかし、そのような通知をあるブラウザから別のブラウザに直接送信することはできません。 –

答えて

2

。プッシュ通知にthisを使用する場合モジュールにはPushNotificationsModule、コンポーネントにはPushNotificationsServiceをインポートする必要があります。

プッシュノーフィクションが動作する前に、ユーザはthemを受け入れる必要があります。

コンポーネント

import { Component } from '@angular/core'; 
import { AngularFire, AuthProviders, AuthMethods, FirebaseListObservable } from 'angularfire2'; 
import { PushNotificationsService } from 'angular2-notifications'; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 

export class AppComponent { 
    items: FirebaseListObservable<any>; 
    name: any; 
    msgVal: string = ''; 

    constructor(
     public af: AngularFire, 
     private _pushNotifications: PushNotificationsService) { 

     this._pushNotifications.requestPermission(); // requestion permission for push notification 

     this.items = af.database.list('/messages', { 
      query: { 
      limitToLast: 5 
      } 
     }); 

     af.database.list('/messages', { 
      query: { 
      limitToLast: 1 
      } 
     }).subscribe((messages: Array<any>) => { 
      // get the last message from messages array 
      this._pushNotifications.create('some title', { body: 'body text' }).subscribe(
      res => { 
       // do something 
      }, 
      err => { 
       // do something 
      } 
     ); 
     }); 

     this.af.auth.subscribe(auth => { 
      if(auth) { 
      this.name = auth; 
      } 
     }); 
    } 
} 
+0

が良好であった。私はこれをコンパイルすることができた:) thanks laot – Pujan

+0

btwこのサービスがonclickイベントを提供するかどうかわかりませんか? 通知をクリックすると希望の場所にリダイレクトされます – Pujan

+0

私は以前はそのパッケージを使ったことがありません。 – Kyrsberg

関連する問題