angular - 在 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。有小费吗?
解决方案
您需要将 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 类之外。
推荐阅读
- node.js - express.session 在通过其域访问应用程序时不会在 iOS Safari 上设置 cookie
- java - 使用 GSON 时更改列表的输出格式
- pyspark - PySpark - 无法按另一列的值过滤行
- reactjs - 无法使用 ReactTestUtils 对 useClickAway 进行单元测试
- excel - 更改多个工作簿查询的单个数据源
- c# - 如何在 GemBox 中使用 2 MultipleBorders 选项
- amazon-web-services - 如何从 Cost Explorer 解析出多个标签?
- java - 覆盖父方法无法正常工作
- android - 当我从字符串数组更改语言时应用程序崩溃
- amazon-web-services - Boto3 API RDS:获取函数接受的参数列表