Appcelerator and Urban Airship - appcelerator-mobile

I am trying out Urban Airship with Appcelerator but I am having problems with using it.
This is my code in App.js:
Will it be "activated" when the user opens the app and then register automatically with Urban Airship?
UrbanAirship = require('ti.urbanAirship');
Ti.API.info("module is => "+UrbanAirship);
Ti.include('common/urbanairship.js');
UrbanAirship.key='XXX';
UrbanAirship.secret ='XXX';
UrbanAirship.master_secret='XXX';
UrbanAirship.baseurl = 'https://go.urbanairship.com';
Ti.Network.registerForPushNotifications({
types: [
Ti.Network.NOTIFICATION_TYPE_BADGE,
Ti.Network.NOTIFICATION_TYPE_ALERT,
Ti.Network.NOTIFICATION_TYPE_SOUND
],
success:function(e){
var deviceToken = e.deviceToken;
Ti.API.info('successfully registered for apple device token with '+e.deviceToken);
var params = {
tags: ['version'+Ti.App.getVersion()],
alias: 'testing'
};
UrbanAirship.register(params, function(data) {
Ti.API.debug("registerUrban success: " + JSON.stringify(data));
}, function(errorregistration) {
Ti.API.warn("Couldn't register for Urban Airship");
});
},
error:function(e) {
Ti.API.warn("push notifications disabled: "+e);
},
callback:function(e) {
var a = Ti.UI.createAlertDialog({
title:'New Message',
message:e.data.alert
});
a.show();
}
});

The modules are always hit or miss for me.
Very rarely have they worked without a lot of troubleshooting.
I've gone the old-school route and it has worked fine for me - although I did change urbanairship.js to add eventlisteners on windowfocus to the register/alias fields in the final app.
But for testing just leave as is. Hope this helps - http://wiki.appcelerator.org/display/guides/Push+Notifications

Related

What is the best way to mock ember services that use ember-ajax in ember-cli-storybook to post and fetch data?

I'm using Ember CLI Storybook to create a story of a component than internally relies upon services that communicate to the internet, to fetch and post information to the backend. The way I'm doing that is using ember-ajax.
I see how to mock an ember model from this section but wondering if there is a workaround for ember ajax service.
I like to use mswjs.io for mocking remote requests. It uses a service worker so you can still use your network log as if you still used your real API.
I have an example repo here showing how to set it up: https://github.com/NullVoxPopuli/ember-data-resources/
But I'll copy the code, in case I change something.
Now, in tests, you'd want something like this: https://github.com/NullVoxPopuli/ember-data-resources/blob/main/tests/unit/find-record-test.ts#L17
module('findRecord', function (hooks) {
setupMockData(hooks);
But since you're using storybook, you'd instead want the contents of that function. (And without the setup/teardown hooks unique to tests)
https://github.com/NullVoxPopuli/ember-data-resources/blob/main/tests/unit/-mock-data.ts#L22
import { rest, setupWorker } from 'msw';
let worker;
export async function setupMockData() {
if (!worker) {
worker = setupWorker();
await worker.start();
// artificial timeout "just in case" worker takes a bit to boot
await new Promise((resolve) => setTimeout(resolve, 1000));
worker.printHandlers();
}
let data = [
{ id: '1', type: 'blogs', attributes: { name: `name:1` } },
{ id: '2', type: 'blogs', attributes: { name: `name:2` } },
{ id: '3', type: 'blogs', attributes: { name: `name:3` } },
];
worker.use(
rest.get('/blogs', (req, res, ctx) => {
let id = req.url.searchParams.get('q[id]');
if (id) {
let record = data.find((datum) => datum.id === id);
return res(ctx.json({ data: record }));
}
return res(ctx.json({ data }));
}),
rest.get('/blogs/:id', (req, res, ctx) => {
let { id } = req.params;
let record = data.find((datum) => datum.id === id);
if (record) {
return res(ctx.json({ data: record }));
}
return res(
ctx.status(404),
ctx.json({ errors: [{ status: '404', detail: 'Blog not found' }] })
);
})
);
}
Docs for msw: https://mswjs.io/

How to use multiple knowledgeBaseId using Dialogflow

We are building a bot using dialogflow in nodejs, we want to know how to utilize multiple knowledgeBase while detecting Intent?
knowledgeBaseFullName = 'unique_id_of_knowledgebase'; // if you don't know how to find it, let me know :)
const knowledgeBasePath = knowbase.knowledgeBasePath(
projectId,
knowledgeBaseFullName
);
... // omitted middle code
const request = {
session: sessionPath,
queryInput: {
text: {
// The query to send to the dialogflow agent
text: 'Hey, how are you doing?',
// The language used by the client (en-US)
languageCode: 'en-US',
},
},
queryParams: {
knowledgeBaseNames: [knowledgeBasePath]
},
};
Here is the answer, today is very very strange for me!
queryParams: {
knowledgeBaseNames: ['projects/project-id/knowledgeBases/k-b-id','projects/project-id/knowledgeBases/k-b-id-2']
},

How dis you get oauth-1.0a and crypto to work with WP-API

I am using crypto and oauth-1.0a from nmp in ionic2 application. I want to access WP-API which is correctly set to handle authentication, I tested this using Postman.
Http.Get results in the following error:
{
"_body": {
"isTrusted": true
},
"status": 0,
"ok": false,
"statusText": "",
"headers": {},
"type": 3,
"url": null
}
The options generated that I pass as argument to Http.Get are as follows:
{“method”:0,“headers”:{“Authorization”:“OAuth oauth_consumer_key=”",
oauth_nonce=“jSZGPwkj4quRGMb0bhBLYKwmc3BGfrQw”, oauth_signature=“x3zseS3XTFBLMsNDLXC4byn2UDI%3D”,
oauth_signature_method=“HMAC-SHA1”, oauth_timestamp=“1522414816”,
oauth_token="",
oauth_version=“1.0"”},“body”:null,“url”:"",“params”:{“rawParams”:"",“queryEncoder”:{},“paramsMap”:{}},“withCredentials”:null,“responseType”:null}
Part of code:
this.oauth = new OAuth({
consumer: {
key: this.apiconstant.consumerkey,
secret: this.apiconstant.consumersecret
},
signature_method: ‘HMAC-SHA1’,
hash_function: hash_function_sha1,
realm:’’
});
let request_data = {
url: ‘’,
method: ‘GET’
};
let token={
key: this.apiconstant.token,
secret: this.apiconstant.tokensecret
}
//This part doesn’t seem to work
this.authkey = this.oauth.authorize(request_data,token);
this.keyoauth = new URLSearchParams();
for (let param in this.authkey) {
this.keyoauth.set(param, this.authkey[param]);
}
let options = new RequestOptions({
method: ‘GET’,//request_data.method
url: ‘’,
headers: this.oauth.toHeader(this.oauth.authorize(request_data,token)),
search: this.keyoauth
});
this.http.get(’’,options)
.map(res => res.json()).subscribe(data=>{
console.log(‘Resulting data’ + JSON.stringify(data));
},
error=>{
console.log(‘Got error’+JSON.stringify(error));
});
//Error part executed
What am I missing here? I’m testing my app on android device. Without authentication I get desired results from the WP-API (Wordpress), that is if the Oauth is disabled on WP-API.
Please help! This is my second day on this. I should also let you know I’m new on these technologies but I’m able to research and understand how they work.

ember-cli mocks doesn't return single record

I'm building a simple ember-cli app. And I tried to implement new mocks feature.
server/mocks/users.js
module.exports = function (app) {
var express = require('express');
var usersRouter = express.Router();
usersRouter.get('/', function (req, res) {
res.send({"users": [
{
"id": 1,
"login": 'berdof'
},
{
"id": 2,
"login": 'berdof2'
}
]});
});
app.use('/api/users', usersRouter);
};
Next when I'm trying to fetch the data, for instance in browser, I just open
http://0.0.0.0:4200/api/user/
and get the correct data. But when I want to fetch a single record it just throws me an error
Cannot GET /api/users/1
Do you know why's that? I've been fighting with this bug for about 2 hours.
Thanks you for any help! Have a good day!
Update
Here's the modified answer according to #kaungst post:
module.exports = function (app) {
var express = require('express');
var router = express.Router();
var dataToSend = [
{
id: 1,
login: 'berdof',
},
{
id: 1,
login: 'berdof2'
}
];
router.get('/', function (req, res) {
res.send({"users": dataToSend});
});
router.get('/:id', function (req, res) {
var id = req.params.id;
res.send({"user": dataToSend[id]});
});
app.use('/api/users', router);
};
you have to create an additional mock to handle that case
for this specific example run
ember g http-mock user
which should generate the following in <\app-name>/server/mocks/user.js
module.exports = function (app) {
var express = require('express');
var userRouter = express.Router();
userRouter.get('/', function (req, res) {
res.send({"user": []});
});
app.use('/api/user', userRouter);
};
alter it to look like this
module.exports = function (app) {
var express = require('express');
var userRouter = express.Router();
userRouter.get('/', function (req, res) {
// this part is hacky but works
// main thing is to decide what you want to send
// happy to update if someone knows a better solution
var url = req.baseUrl.split('/');
var id = parseInt(url[url.length-1]);
var users = [{
"id": 1,
"login": 'berdof'
},
{
"id": 2,
"login": 'berdof2'
}];
res.send({"user": users[id]});
});
app.use('/api/user/:id', userRouter);
};
Better alternative to http-mocks
I would recommend using ember-cli-mirage for client-side development and testing.
The project website provides enough detailed documentation
Benefits
It creates an in-memory db which supports all the CRUD operations.
Use the same data for both development and testing.
Integrates well with faker.js to create random data.
Ability to create fixtures.
Having struggled with using http-mocks, I would highly recommend it.

Can the Knockout Concurrency plugin track newly added or deleted rows?

I'm trying to use the Knockout Concurrency plugin in my project, and I'm currently fiddling with the example code, but I'm not getting it to work:
https://github.com/AndersMalmgren/Knockout.Concurrency/wiki/Getting-started
ViewModel = function() {
this.name = ko.observable("John").extend({ concurrency: true});
this.children = [{ name: ko.observable("Jane").extend({concurrency: true })}, { name: ko.observable("Bruce").extend({concurrency: true })}];
this.getData = function() {
//Simulate backend data
var data = { name: "John Doe", children: [{ name: "Jane Doe"},{ name: "Bruce Wayne"}, { name: "New row"}]};
new ko.concurrency.Runner().run(this, data);
}
}
ko.applyBindings(new ViewModel());
http://jsfiddle.net/rCVk4/3/
Nothing happens and the newly added item is not tracked by the plugin, does anyone know why?
Thanks for trying out my Plugin, really fast too, I uploaded the code today!
The plugin does indeed support tracking of deleted and added rows. But for it to know which rows are what It needs you to supply it with a mapper
var mappings = {
children: {
key: function(item) {
return ko.utils.unwrapObservable(item.id);
},
create: function(data) {
return { id: data.id, name: data.name };
}
}
};
The name children corresponds to the name of the array.
The Key method is used to identify the property used as an identifier.
The Create method is used to create new rows (Added rows).
You can download the MVC3 sample from Github for a fully featured Demo, also please try out this Fiddle
http://jsfiddle.net/7atZT/