简体   繁体   English

在Angular 6和Phonegap上推送通知

[英]Push Notifications on Angular 6 and Phonegap

I have to implement push notifications handling in app written in Angular 6 and Phonegap. 我必须在用Angular 6和Phonegap编写的应用程序中实现推送通知处理。 I installed phonegap-push-plugin and generally it works, when I use it in index.html: 我安装了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 :(
  });
}

I get notifications, system displays it, but now I have to go to eg. 我收到通知,系统显示它,但是现在我必须去例如。 news page with id from notification. ID来自通知的新闻页面。 I can't do this via window.location, and when I try to declare var PushNotification: any; 我无法通过window.location以及尝试声明var PushNotification时做到这一点:any; in app.component constructor, TS tells me, that PushNotification is not declared. 在app.component构造函数中,TS告诉我未声明PushNotification。 Any tips? 有小费吗?

You need to wrap platformBrowserDynamic().bootstrapModule(AppModule) in onDeviceReady in main.ts: 您需要在main.ts的onDeviceReady中包装platformBrowserDynamic()。bootstrapModule(AppModule):

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

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

Otherwise AppModule is firing before cordova plugin is loaded, that's why u have PushNotification undefined. 否则,将在加载cordova插件之前触发AppModule,这就是为什么您未定义PushNotification的原因。

Second thing is: 第二件事是:

I try to declare var PushNotification: any; 我尝试声明var PushNotification:any; in app.component constructor 在app.component构造函数中

declare let PushNotification: any; should be outside of AppComponent class. 应该在AppComponent类之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM