首页 > 解决方案 > 在 Angular 6 和 Phonegap 上推送通知

问题描述

我必须在用 Angular 6 和 Phonegap 编写的应用程序中实现推送通知处理。我安装了phonegap-push-plugin,当我在index.html中使用它时,它通常可以工作:

function onLoad() {
  document.addEventListener("deviceready", onDeviceReady, false);
  if (device.platform === 'Android') {
    setPushNotifications();
  } else {
    pushID = null;
  }
}

function setPushNotifications() {
  push = PushNotification.init({
    "android": {
      "senderID": "xxx",
      "icon": "notify_icon",
      "iconColor": "#1f5382"
    },
    "ios": {
      "sound": true,
      "vibration": true,
      "badge": true
    },
  });

  push.on('registration', (data) => {
    pushID = data.registrationId;
  });
  push.on('notification', data => {
    alert('PUSH arrived!');
    // go to another view in Angular... HOW :(
  });
}

我收到通知,系统显示它,但现在我必须去例如。带有来自通知的 id 的新闻页面。我无法通过 window.location 执行此操作,并且当我尝试声明 var PushNotification: any; 在 app.component 构造函数中,TS 告诉我,未声明 PushNotification。有小费吗?

标签: angularpush-notificationphonegapphonegap-plugin-push

解决方案


您需要将 platformBrowserDynamic().bootstrapModule(AppModule) 包装在 main.ts 中的 onDeviceReady 中:

const onDeviceReady = () => {
  platformBrowserDynamic().bootstrapModule(AppModule)
    .catch(err => console.log(err));
}

document.addEventListener('deviceready', onDeviceReady, false);

否则 AppModule 在加载cordova插件之前触发,这就是你未定义 PushNotification 的原因。

第二件事是:

我尝试声明 var PushNotification: any; 在 app.component 构造函数中

declare let PushNotification: any;应该在 AppComponent 类之外。


推荐阅读