I'm trying to set up a very basic example of push notifications on android. I use nativescript + typescript (even if the code is a bit messy, as i don't understand how to correctly rewrite "var Observable = require("data/observable");" and "viewModel" in typescript. The code is based on this example https://bradmartin.net/2015/12/28/use-google-cloud-messaging-for-push-notifications-with-nativescript/
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>
<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="{{ registrationId }}" class="title" textWrap="true"></TextView>
<Label text="{{ message }}" class="message" textWrap="true" ></Label>
</StackLayout> `
})
export class AppComponent {
// Your TypeScript logic goes here
viewModel = new Observable.Observable({
registrationId: ""
});
pageLoaded(args) {
var page = args.object;
page.bindingContext = this.viewModel;
}
registerTap (args) {
var settings = {
// Android settings
senderID: '0434blablabla', // Android: Required setting with the sender/project number
notificationCallbackAndroid: function(message) { // Android: Callback to invoke when a new push is received.
console.log(JSON.stringify(message));
alert(JSON.stringify(message));
},
// 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: function(message) {
alert(JSON.stringify(message));
}
};
pushPlugin.register(settings,
// Success callback
function(token) {
alert("test");
// if we're on android device we have the onMessageReceived function to subscribe
// for push notifications
if(pushPlugin.onMessageReceived) {
pushPlugin.onMessageReceived(settings.notificationCallbackAndroid);
}
alert('Device registered successfully : ' + token);
this.viewModel.set("regId", token);
},
// Error Callback
function(error){
alert("errore");
console.log(error);
alert(error);
}
);
}
}
when i click on the register button, i get the following error:
TypeError: "Cannot read property 'set' of undefined" I'm new to Typescript and notifications handling, can you give me a hint?
thanks in advance
this.viewModel
包含registrationId
但是您将regId
更改为其中之一,它应该可以工作
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.