如何初始化推送通知

时间:2017-01-20 04:41:41

标签: angular push-notification ionic2

我正在关注此https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/TYPESCRIPT.md guidline

在我的app.components.ts文件中

import { Push, PushToken } from '@ionic/cloud-angular';

constructor(platform: Platform) {
    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();
    });

    var push = PushNotification.init({
        android: {
            senderID: '1234XXX9' //GCM project number
        },
        ios: {
            alert: 'true',
            badge: true,
            sound: 'false'
        },
        windows: {}
    });
    push.on('registration', (data) => {
        console.log(data.registrationId);
    });

    push.on('notification', (data) => {
        console.log(data.message);
        console.log(data.title);
        console.log(data.count);
        console.log(data.sound);
        console.log(data.image);
        console.log(data.additionalData);
    });

    push.on('error', (e) => {
        console.log(e.message);
    });
  }

我在这一行var push = Push.init({

上收到错误

当我给出离子构建android时,我在上面的行上出现错误错误。

如果我遗漏任何积分,请告诉我。

错误

[10:18:05]  typescript: src/app/app.component.ts, line: 24 
            Cannot find name 'PushNotification'. 

      L24:      var push = PushNotification.init({
      L25:          android: {

[10:18:05]  transpile failed 
[10:18:05]  ionic-app-script task: "build" 
[10:18:05]  Error: Error 

1 个答案:

答案 0 :(得分:1)

推送您使用的是来自Ionic的云服务。

要使用离子云插件,您必须使用离子云设置项目。 Setup steps here

检查推送特定文档here。 你应该注入它而不是尝试全局变量。

constructor(platform: Platform, private push:Push) {
    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.
    this.push.register().then((t: PushToken) => {
        return this.push.saveToken(t);
    }).then((t: PushToken) => {
       console.log('Token saved:', t.token);
    });
      StatusBar.styleDefault();
      Splashscreen.hide();
    });