2017-03-22 1 views
1

2 번과 Firebase에 상당히 익숙하지만 채팅 앱 here을 생성 할 수있었습니다. 채팅 앱에 새 메시지가있을 때 푸시 알림을 만드는 것이 최종 목표입니다. 채팅 수신시 푸시 알림 만들기

  1. 나는 this를 설치하려하지만 난이 문서를 이해하지 수 있기 때문에 매우 어렵다.

  2. this thread에서 해결책을 시도했습니다. 하지만 앱은 알림을 감지하지 못합니다.

이 권한에 접근하는 방법에 대한 도움이 필요하십니까? , 당신의 구성 요소의 메시지 데이터베이스에 등록 할

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

당신은 * 장치 *에 * 다른 장치 *에서 직접 중포 기지 클라우드 메시징 메시지를 보낼 수 없습니다. 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

이 내 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 파일이 포함되어 있습니다. 푸시 알림에 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

이 좋았습니다. 나는이 컴파일 할 수 있었다 :) 감사 laot – Pujan

+0

btw 어떤 아이디어이 서비스가 onclick 이벤트를 제공한다면? 알림을 클릭하면 원하는 장소로 리디렉션됩니다. – Pujan

+0

나는 그 패키지를 사용 해보지 않았습니다. – Kyrsberg

관련 문제