Android上不调用onMessageReceived回调
在一个简单的app.component.ts示例中,我有以下nativescript+typescript+angular代码。问题是从未调用settings.notificationCallbackAndroid,我在控制台中没有看到任何输出。注册过程正确完成,我收到令牌并可以获得推送通知,但android回调不会触发。。有什么提示吗Android上不调用onMessageReceived回调,android,typescript,push-notification,nativescript,Android,Typescript,Push Notification,Nativescript,在一个简单的app.component.ts示例中,我有以下nativescript+typescript+angular代码。问题是从未调用settings.notificationCallbackAndroid,我在控制台中没有看到任何输出。注册过程正确完成,我收到令牌并可以获得推送通知,但android回调不会触发。。有什么提示吗 import { Component } from "@angular/core"; import * as pushPlugin from "nativesc
import { Component } from "@angular/core";
import * as pushPlugin from "nativescript-push-notifications";
var Observable = require("data/observable");
@Component({
selector: "my-app",
template: `
<ActionBar title="My App" icon="" class="action-bar">
</ActionBar>
<StackLayout (loaded)="pageLoaded()">>
<Label text="Tap the button to trigger the register function." textWrap="true" class=""></Label>
<Button text="REGISTER" (tap)="registerTap()" ></Button>
<label text="Your device id/token:" textWrap="true" ></label>
<TextView text={{viewModel.registrationId}} class="title" textWrap="true"></TextView>
<Label text="{{ message }}" class="message" textWrap="true" ></Label>
</StackLayout>
`
})
export class AppComponent {
viewModel = new Observable.Observable({
registrationId: ""
});
handleMessage(){
alert('handle');
}
pageLoaded() {
this.viewModel.set("registrationId", "");
}
//args
registerTap () {
let settings = {
// Android settings
senderID: 'xxxxxxxxxxx', // Android: Required setting with the sender/project number
notificationCallbackAndroid: (message, data, notification) => {
console.log("MESSAGE: " + JSON.stringify(message));
console.log("DATA: " + JSON.stringify(data));
console.log("NOTIFICATION: " + JSON.stringify(notification));
},
// iOS settings
badge: true, // Enable setting badge through Push Notification
sound: true, // Enable playing a sound
alert: true, // Enable creating a alert
// Callback to invoke, when a push is received on iOS
notificationCallbackIOS: message => {
//alert(JSON.stringify(message));
}
};
pushPlugin.register(settings,
// Success callback
//equivale a function(token)
token => {
// if we're on android device we have the onMessageReceived function to subscribe
// for push notifications
if(pushPlugin.onMessageReceived) {
alert("sono qui");
pushPlugin.onMessageReceived(
settings.notificationCallbackAndroid
);
}
// alert('Device registered successfully : ' + token);
this.viewModel.set("registrationId", token);
alert('Device registered successfully : '+ this.viewModel.registrationId);
},
// Error Callback
error =>{
alert("errore");
//console.log(error);
alert(error);
}
);
}
}
从“@angular/core”导入{Component};
从“nativescript推送通知”导入*作为推送插件;
var可观测=要求(“数据/可观测”);
@组成部分({
选择器:“我的应用程序”,
模板:`
>
`
})
导出类AppComponent{
viewModel=新的可观察的。可观察的({
注册ID:“
});
handleMessage(){
警报(“句柄”);
}
pageLoaded(){
this.viewModel.set(“registrationId”,即“”);
}
//args
registerTap(){
让设置={
//安卓设置
senderID:'XXXXXXXXXX',//Android:需要设置发件人/项目编号
notificationCallbackAndroid:(消息、数据、通知)=>{
log(“消息:+JSON.stringify(消息));
log(“数据:+JSON.stringify(数据));
log(“通知:+JSON.stringify(通知));
},
//iOS设置
badge:true,//通过推送通知启用设置badge
声音:true,//启用播放声音
警报:true,//启用创建警报
//在iOS上接收推送时调用的回调
notificationCallbackIOS:message=>{
//警报(JSON.stringify(message));
}
};
pushPlugin.register(设置、,
//成功回调
//等价于函数(令牌)
令牌=>{
//如果我们在安卓设备上,我们可以订阅onMessageReceived函数
//用于推送通知
if(pushPlugin.onMessageReceived){
警惕(“索诺基”);
pushPlugin.onMessageReceived(
settings.notificationCallbackAndroid
);
}
//警报('成功注册的设备:'+令牌);
this.viewModel.set(“registrationId”,token);
警报('设备已成功注册:'+this.viewModel.registrationId);
},
//错误回调
错误=>{
警报(“错误”);
//console.log(错误);
警报(错误);
}
);
}
}
将应用程序放在后台或前台会有所不同吗?i、 e.如果应用程序被前景化,这种情况还会发生吗?是的,很遗憾。我应该补充一点,我正在模拟器上运行它