【问题标题】:ionic2 push notification error : device has not fired after 5 secondsionic2 推送通知错误:5 秒后设备未触发
【发布时间】:2017-01-31 14:54:23
【问题描述】:

我正在尝试在最新的 ionic2 版本中实现推送通知,这是我遵循此文档的代码:https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59#.2oe9zk3z5

app.component.ts

import { Component, ViewChild } from '@angular/core';
import { Platform, Nav, AlertController } from 'ionic-angular';
import { StatusBar, Splashscreen, AdMob, Push } from 'ionic-native';

import { HomePage } from '../pages/home/home';
import { Config } from '../providers/config';

import { DownloadPage } from '../pages/download/download'

@Component({
  templateUrl: 'app.html'
})
export class MyApp {    
  @ViewChild(Nav) nav: Nav;
  rootPage = HomePage;

  constructor(public platform: Platform, public conf: Config, public alertCtrl: AlertController) {

    platform.ready().then(() => {
      StatusBar.styleDefault();
      Splashscreen.hide();

        AdMob.createBanner({adId : this.conf.admobBanner, isTesting:true}).then(() =>   { AdMob.showBanner(8); });

      this.initPushNotification();


    });

  }

    initPushNotification(){
    if (!this.platform.is('cordova')) {
      console.warn("Push notifications not initialized. Cordova is not available - Run in physical device");
      return;
    }
    let push = Push.init({
      android: {
        senderID: "xxxxxx" // I have senderID
      },
      ios: {
        alert: "true",
        badge: false,
        sound: "true"
      },
      windows: {}
    });

    push.on('registration', (data) => {
      console.log("device token ->", data.registrationId);
      //TODO - send device token to server
    });
    push.on('notification', (data) => {
      console.log('message', data.message);
      let self = this;
      //if user using app and push notification comes
      if (data.additionalData.foreground) {
        // if application open, show popup
        let confirmAlert = this.alertCtrl.create({
          title: 'New Notification',
          message: data.message,
          buttons: [{
            text: 'Ignore',
            role: 'cancel'
          }, {
            text: 'View',
            handler: () => {
              //TODO: Your logic here
              self.nav.push(DownloadPage);
            }
          }]
        });
        confirmAlert.present();
      } else {
        //if user NOT using app and push notification comes
        //TODO: Your logic on click of push notification directly
        self.nav.push(DownloadPage);
        console.log("Push notification clicked");
      }
    });
    push.on('error', (e) => {
      console.log(e.message);
    });
  }
}

我已经获得了 FCM 服务器密钥和 senderID 并设置了 ionic Icloud

所以当我在我的 android 设备上运行它时,它会在控制台中显示此错误:

[14:41:35]  console.log: deviceready has not fired after 5 seconds. 
[14:41:35]  console.log: Channel not fired: onPluginsReady 
[14:41:35]  console.log: Channel not fired: onCordovaReady 
[14:41:36]  error opening ws message: {"category":"console","type":"log","data":["DEVICE READY FIRED AFTER",4182,"ms"]} 
[14:41:47]  error opening ws message: {"category":"console","type":"log","data":["device token 
            ->","cJ5Iw6b9OpW:APA91bHmplWN80qKFld0wtcfnFCmO5kjFHj1tuCwMkCOMKYcZ-HbMC4i7Vg1hIbdL9d0eDdl2c7MWsJ79XrLQ3m4cnEwj6I7E3s2eDO58yrqg9C_xGsLkLWYWQ"]}

【问题讨论】:

  • 我已经使用 OneSignal API 做了推送通知,离子云的推送通知有很多问题,我认为现在已经解决了。

标签: angular ionic-framework push-notification ionic2 push


【解决方案1】:

你可以改变:

console.log("device token ->", data.registrationId);

到:

console.log("device token:");
console.log(JSON.stringify(data));

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2020-02-19
    • 2019-10-13
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 1970-01-01
    • 2016-01-03
    相关资源
    最近更新 更多