Typescript TypeError:无法读取未定义的属性“set”

时间:2017-06-01 09:39:36

标签: javascript typescript firebase push-notification nativescript

我正在尝试在Android上设置一个非常基本的推送通知示例。我使用nativescript + typescript(即使代码有点乱,因为我不明白如何正确地重写“var Observable = require(”data / observable“);”和“viewModel”in typescript。代码基于在此示例中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);
    }
);
}



}

当我点击注册按钮时,我收到以下错误:

TypeError:“无法读取未定义的属性'set'” 我是打字稿和通知处理的新手,你能给我一个提示吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

this.viewModel包含registrationId,但你设置regId会改变其中一个,它应该有效