我在一个简单的app.component.ts示例中有以下nativescript + typescript + angular代码。问题是从未调用settings.notificationCallbackAndroid,我在控制台中看不到任何输出。注册过程正确完成,我收到令牌,我可以得到推送通知,但没有触发Android回调..任何提示?
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);
}
);
}
}