在接收聊天时创建推送通知

时间:2017-03-22 00:17:26

标签: angular typescript firebase angularfire

我对angular 2和Firebase相当新,但我设法创建了一个聊天应用here,我的最终目标是在聊天应用中有新消息时创建推送通知。

  1. 我尝试安装this,但由于我无法理解该文档,因此非常困难。

  2. 我也尝试了this thread的解决方案。但应用程序不会检测到通知。

  3. 有关如何做到这一点的任何帮助吗?

    这是我的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 { }
    

1 个答案:

答案 0 :(得分:2)

我会订阅组件中的messages数据库。如果您想使用this进行推送通知。您必须在模块中导入PushNotificationsModule并在组件中导入PushNotificationsService

在推送nofications工作之前,用户必须接受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;
          }
        });
    }
 }