kinvey-nativescript-sdk
Kinvey JavaScript SDK for NativeScript 应用。
npm i --save kinvey-nativescript-sdk

Kinvey NativeScript SDK

安装

从命令提示符进入您的应用程序根目录并执行

npm i kinvey-nativescript-sdk

使用方法

初始化 SDK

在应用程序启动之前,我们需要初始化 SDK,因此打开 app.js 并在 application.start(); 之前添加以下代码

JavaScript

import * as Kinvey from 'kinvey-nativescript-sdk';

Kinvey.init({
appKey: '<yourAppKey>',
appSecret: '<yourAppSecret>'
});

TypeScript

import * as Kinvey from 'kinvey-nativescript-sdk';

Kinvey.init({
appKey: '<yourAppKey>',
appSecret: '<yourAppSecret>'
});

Angular

在您的 app.module.ts 中导入 KinveyModule 如下以初始化 SDK

import { NgModule } from '@angular/core';
import { KinveyModule } from 'kinvey-nativescript-sdk/angular';

@NgModule({
imports: [
KinveyModule.init({
appKey: '<yourAppKey>',
appSecret: '<yourAppSecret>'
})
]
})
export class AppModule { }

然后您可以使用依赖注入在模块中注入 Kinvey 服务,如下所示

import { Component } from '@angular/core';
import { UserService } from 'kinvey-nativescript-sdk/angular';

@Component()
export class AppComponent {
constructor(private userService: UserService) {}

async login() {
try {
const user = await this.userService.login('<username>', '<password>');
console.log(user);
} catch (error) {
console.log(error);
}
}
}

以下服务可用于依赖注入使用

  • DataStoreService
  • EndpointService
  • FilesService
  • PingService
  • UserService

Push

您需要安装 nativescript-plugin-firebase 并遵循 https://github.com/EddyVerbruggen/nativescript-plugin-firebase#prerequisites 上的说明来设置您的应用程序。请确保在您的 app.ts 文件中引入 nativescript-plugin-firebase 插件,如示例应用程序所示 示例应用程序

然后您可以使用 Push 模块注册运行您的应用程序的设备,如下所示

import * as Push from 'kinvey-nativescript-sdk/push';

function receivedPushNotificaiton(message) {
console.log("Title: " + message.title);
console.log("Body: " + message.body);
// if your server passed a custom property called 'foo', then do this:
console.log("Value of 'foo': " + message.data.foo);
}

Push.register(receivedPushNotification)
.then((deviceToken) => {
console.log(`The device with device token ${deviceToken} is registered for push.`);
})
.catch((error) => {
console.log(error);
})

要注销运行您的应用程序的设备,请这样做

import * as Push from 'kinvey-nativescript-sdk/push';

Push.unregister()
.then((deviceToken) => {
console.log(`The device with device token ${deviceToken} has been unregistered for push.`);
})
.catch((error) => {
console.log(error);
})
Angular

您需要在您的 app.module.ts 中导入 KinveyPushModule,如下所示

import { NgModule } from '@angular/core';
import { KinveyModule } from 'kinvey-nativescript-sdk/angular';
import { KinveyPushModule } from 'kinvey-nativescript-sdk/angular/push';

@NgModule({
imports: [
KinveyModule.init({
appKey: '<yourAppKey>',
appSecret: '<yourAppSecret>'
}),
KinveyPushModule
]
})
export class AppModule { }

然后您可以在模块中注入 PushService,如下所示

import { Component } from '@angular/core';
import { PushService } from 'kinvey-nativescript-sdk/angular/push';

@Component()
export class AppComponent {
constructor(private pushService: PushService) {}

receivedPushNotificaiton(message) {
console.log("Title: " + message.title);
console.log("Body: " + message.body);
// if your server passed a custom property called 'foo', then do this:
console.log("Value of 'foo': " + message.data.foo);
}

async registerForPush() {
try {
const deviceTokne = await this.pushService.register(this.receivedPushNotification);
console.log(`The device with device token ${deviceToken} has been unregistered for push.`);
} catch (error) {
console.log(error);
}
}
}

构建

如果您想自己构建 SDK,请克隆 monorepo,然后

  • npm i
  • npm run build

然后您可以运行 npm i /<localpath>/packages/nativescript-sdk 来安装 SDK 构建